JDK17新特性

一.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 Shape
    permits 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 Expr
    permits 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 Shape
    permits 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未来要干的活

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我才是真的封不觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值