gpt4 book ai didi

java - JDK 15 密封类 - 如何跨包使用?

转载 作者:行者123 更新时间:2023-12-04 11:23:25 26 4
gpt4 key购买 nike

我有一个简单的密封类,MyShape :

public sealed class MyShape permits MyCircle {

private final int width;
private final int height;

public MyShape(int width, int height) {
this.width = width;
this.height = height;
}

public int width() {
return width;
}

public int height() {
return height;
}
}
还有一个简单的子类, MyCircle :
public final class MyCircle extends MyShape {

public MyCircle(int width) {
super(width, width);
}
}
当两个类都在同一个包中时,一切都会编译并运行。如果我将 MyCircle 移动到子包中,则构建会中断: java: class is not allowed to extend sealed class: org.example.MyShape .
我的理解来自 JDK 15 docs是这应该工作。我错过了一步吗?
我已经 created a GitHub repo如果你想尝试。

最佳答案

如您链接的文档中所述:
JDK 15 Documentation

They must be in the same module as the sealed class (if the sealed class is in a named module) or in the same package (if the sealed class is in the unnamed module).

关于java - JDK 15 密封类 - 如何跨包使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63946541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com