“sealed” 和 “final” 类在Java中的区别

huangapple go评论81阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月25日 17:44:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64061710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定