https://github.com/kunminx/sealedclass4java
让 Java 1.8 也能像 Kotlin 一样使用 Sealed Class。Make Java 1.8 use Sealed Class like Kotlin
https://github.com/kunminx/sealedclass4java
java java-8 sealed-class
Last synced: 6 months ago
JSON representation
让 Java 1.8 也能像 Kotlin 一样使用 Sealed Class。Make Java 1.8 use Sealed Class like Kotlin
- Host: GitHub
- URL: https://github.com/kunminx/sealedclass4java
- Owner: KunMinX
- License: apache-2.0
- Created: 2022-08-29T15:17:03.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-31T03:11:29.000Z (about 3 years ago)
- Last Synced: 2025-03-26T12:22:05.350Z (6 months ago)
- Topics: java, java-8, sealed-class
- Language: Java
- Homepage:
- Size: 113 KB
- Stars: 11
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

研发故事:[《Kotlin Sealed Class 太香了,Java 8 也想用怎么办?》](https://juejin.cn/post/7137571636781252622/)
## 依赖
项目根目录 build.gradle 添加如下依赖:
```
allprojects {
repositories {
// ...
maven { url 'https://www.jitpack.io' }
}
}
```模块 build.gradle 添加如下依赖:
```groovy
implementation 'com.github.KunMinX:SealedClass4Java:1.4.0-beta'
```
## 使用说明
1.创建一个接口,添加 SealedClass 注解,且接口名开头 _ 下划线,
```java
@SealedClass
public interface _TestEvent {
void resultTest1(String a, int b);
void resultTest2(String a, int b, int c);
}
```2.编译即可生成目标类,例如 TestEvent,然后可以像 Kotlin 一样使用该类:
```java
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 KunMinXLicensed 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 athttp://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.
```