问题描述
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。