JDK17新特性
创始人
2024-02-16 12:23:19
0

一.JEP 409: Sealed Classes

1.1简介

官网链接sealed class

从如下官网的简介与描述可以看出,这个新特性的目的是为了限制类与接口的 被继承与实现,比如说我有个 A类,那么我现在限定只有 B,C,D三个类可以继承 那么就需要用到这个新特性了

 这个在JDK15,16中都作为了预览特性,在jdk17中正式发版 并且在jdk16后没有改动

1.2使用方式

The classes specified by permits must be located near the superclass: either in the same module (if the superclass is in a named module) or in the same package (if the superclass is in the unnamed module).

When the permitted subclasses are small in size and number, it may be convenient to declare them in the same source file as the sealed class

如上两段话摘自官网 意思是说使用时 父子类的几种位置关系 

1.2.1非匿名模块在相同模块中即可

package com.example.geometry;public abstract sealed class Shapepermits Circle, Rectangle, Square { ... }

1.2.2匿名模块需要在同一package

package com.example.geometry;public abstract sealed class Shape permits com.example.polar.Circle,com.example.quad.Rectangle,com.example.quad.simple.Square { ... }

1.2.3当子类代码非常精简时 直接内部类也行 

abstract sealed class Root { ... final class A extends Root { ... }final class B extends Root { ... }final class C extends Root { ... }
}

1.3三个限制

第一点 就是上面的1.2的前两点

第二点 每个permitted 子类 必须要直接继承 sealed 父类

第三点  permitted 子类 必须有三个修饰符之一 且不能同时存在

final(或者直接用record类):这个意思大家都懂 除此之外 因为 record数据类(顾名思义 他就是个用来传输数据 没有啥业务相关方法的类) 默认是final修饰 所以也能继承

package com.example.expression;public sealed interface Exprpermits ConstantExpr, PlusExpr, TimesExpr, NegExpr { ... }public record ConstantExpr(int i)       implements Expr { ... }
public record PlusExpr(Expr a, Expr b)  implements Expr { ... }
public record TimesExpr(Expr a, Expr b) implements Expr { ... }
public record NegExpr(Expr e)           implements Expr { ... }

sealed:用这个修饰符修饰的类 必须有子类 且要用 permitt 关键字后指明 如下报错可以看出

 non-sealed 这个就跟上面这个相反 用这个修饰符修饰后 就不能指定 特定的类继承 该类了,并且未知的类就可以对他进行继承了

示例代码如下

package com.example.geometry;public abstract sealed class Shapepermits Circle, Rectangle, Square, WeirdShape { ... }public final class Circle extends Shape { ... }public sealed class Rectangle extends Shape permits TransparentRectangle, FilledRectangle { ... }
public final class TransparentRectangle extends Rectangle { ... }
public final class FilledRectangle extends Rectangle { ... }public final class Square extends Shape { ... }public non-sealed class WeirdShape extends Shape { ... }  

 

1.4Sealed classes and conversions

 这里不多说了 

1.4.1instanceof前为普通类实例

使用 instanceof 前面那个实例所属类 如果是普通类 那么 编译通过 正常打印输出 如下demo

1.4.2instanceof前为final类实例

可以看到 当 C类没有实现了 I 接口时 编ins译报错 ,实现了的话就不会报错,因为这涉及到了一个强制转换的问题

1.4.3instanceof前为sealed类时

permits子类中全为 final修饰 编译报错 

反之,permits子类中不全为 final修饰 编译通过

1.5 JDK中的密封类

1.6switch模式匹配结合sealed class

下图中意思简要来说就是 我们不需要用if else来 结合instance of 判断,我们通过第二段代码所示的,使用switch case结合 密封类时 不需要写 default,并且如果少了某个子类的case时 会编译报错

1.7因为新增类修饰符而 class文件中多出了几个属性

1.8反射api中Class类中多出了俩方法

1.9未来要干的活

 

 

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...