英文:
Difference between sealed and final class in Java
问题
我刚刚看到 Java 15 应该会有 sealed
类。jep360 表示:
> Sealed 类和接口限制了哪些其他类或接口可以扩展或实现它们。
我认为这正是 Java 中的 final 类
所做的事情。所以现在我想知道:final 类
和 sealed 类
之间有什么区别?
英文:
I have just read that Java 15 should have sealed
classes.
jep360 says:
> Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.
I thought that this is exactly what a final class
does in Java.
So now I wonder: What is the difference between a final class
and a sealed class
?
答案1
得分: 23
final class A {...}
意味着不允许任何类继承A
。
sealed class A permits B {...}
意味着只有B
可以继承A
,但其他类不允许这样做。
英文:
final class A {...}
means that no class is allowed extend A
.
sealed class A permits B {...}
means that only B
can extend A
but no other class is allowed to do that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论