登录

去注册 忘记密码?

登录

注册

去登录

  • 扫码关注公众号
  • 发送“我爱安卓
  • 即可获取验证码

注册

解锁回答区域

  • 扫码关注公众号
  • 发送“我爱安卓

若你登陆,将永久解锁;
若未登录,仅本机解锁。

解锁回答区域

获取注册验证码

  • 扫码关注公众号
  • 发送“我爱安卓
  • 即可获取验证码

Kotlin Sealed Class 太香了,Java 8 也想用怎么办?

KunMinX   2022-09-04 22:26   收藏

 

研发故事:《Kotlin Sealed Class 太香了,Java 8 也想用怎么办?》

 

依赖

项目根目录 build.gradle 添加如下依赖:

allprojects {
    repositories {
        // ...
        maven { url 'https://www.jitpack.io' }
    }
}

模块 build.gradle 添加如下依赖:

implementation 'com.github.KunMinX:SealedClass4Java:1.4.0-beta'

 

使用说明

1.创建一个接口,添加 SealedClass 注解,且接口名开头 _ 下划线,

@SealedClass
public interface _TestEvent {
  void resultTest1(String a, int b);
  void resultTest2(String a, int b, int c);
}

2.编译即可生成目标类,例如 TestEvent,然后可以像 Kotlin 一样使用该类:

TestEvent event = TestEvent.ResultTest1("textx");
switch (event.id) {
  case TestEvent.ResultTest1.ID:
    TestEvent.ResultTest1 event1 = (TestEvent.ResultTest1) event;
    event1.copy(1);
    event1.paramA;
    event1.resultB;
    break;
  case TestEvent.ResultTest2.ID:
    break;
}

3.进阶使用详见 https://juejin.cn/post/7137571636781252622#heading-2

 

License

Copyright 2019-present KunMinX

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

项目地址:https://github.com/KunMinX/Java8-Sealed-Class