英文:
Convert Kotlin class to Java
问题
以下是已经翻译好的内容:
public abstract class RepoSearchResult {
public static class Success extends RepoSearchResult {
// 在这里添加必要的属性和方法
}
public static class Error extends RepoSearchResult {
// 在这里添加必要的属性和方法
}
}
英文:
How to convert this Kotlin class to Java
sealed class RepoSearchResult {
data class Success(val data: List<Repo>) : RepoSearchResult()
data class Error(val error: Exception) : RepoSearchResult()
}
<sup>code source: github</sup>
I created this but I don't know if it's correct, and if it is how to return data from it?
public abstract class RepoSearchResult {
public static class Success extends RepoSearchResult {
}
public static class Error extends RepoSearchResult {
}
}
答案1
得分: 1
使用IntelliJ将该类反编译为Java产生以下结果。我将Repo
更改为String
,以便在本地编译时不需要依赖项。
@元数据(
mv = {1, 4, 0},
bv = {1, 0, 3},
k = 1,
d1 = {"\u0000\u0016\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b6\u0018\u00002\u00020\u0001:\u0002\u0003\u0004B\u0007\b\u0002¢¢\u0006\u0002\u0010\u0002¸\u0001\u0002\u0005\u0006¸\u0006\u0007"},
d2 = {"LRepoSearchResult;", "", "()V", "错误", "成功", "LRepoSearchResult$Success;", "LRepoSearchResult$Error;", "test_kotlin"}
)
public abstract class RepoSearchResult {
private RepoSearchResult() {
}
// $FF: synthetic method
public RepoSearchResult(DefaultConstructorMarker $constructor_marker) {
this();
}
@元数据(
mv = {1, 4, 0},
bv = {1, 0, 3},
k = 1,
d1 = {"\u0000*\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010 \n\u0002\u0010\u000e\n\u0002\b\u0006\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0000\b\b\u0018\u00002\u00020\u0001B\u0013\u0012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¸\u0006\u0002\u0010\u0005J\u000f\u0010\b\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003HÇ\u0003J\u0019\u0010\t\u001a\u00020\u00002\u000e\b\u0002\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003HÇ\u0001J\u0013\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rHÖ\u0003J\t\u0010\u000e\u001a\u00020\u000fHÖ\u0001J\t\u0010\u0010\u001a\u00020\u0004HÖ\u0001R\u0017\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¸\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007¸\u0006\u0011"},
d2 = {"LRepoSearchResult$Success;", "LRepoSearchResult;", "data", "", "LRepoSearchResult;", "(Ljava/util/List;)V", "getData", "()Ljava/util/List;", "component1", "copy", "equals", "", "other", "hashCode", "", "toString", "test_kotlin"}
)
public static final class Success extends RepoSearchResult {
@NotNull
private final List data;
@NotNull
public final List getData() {
return this.data;
}
public Success(@NotNull List data) {
Intrinsics.checkParameterIsNotNull(data, "data");
super((DefaultConstructorMarker)null);
this.data = data;
}
@NotNull
public final List component1() {
return this.data;
}
@NotNull
public final RepoSearchResult.Success copy(@NotNull List data) {
Intrinsics.checkParameterIsNotNull(data, "data");
return new RepoSearchResult.Success(data);
}
// $FF: synthetic method
public static RepoSearchResult.Success copy$default(RepoSearchResult.Success var0, List var1, int var2, Object var3) {
if ((var2 & 1) != 0) {
var1 = var0.data;
}
return var0.copy(var1);
}
@NotNull
public String toString() {
return "Success(data=" + this.data + ")";
}
public int hashCode() {
List var10000 = this.data;
return var10000 != null ? var10000.hashCode() : 0;
}
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof RepoSearchResult.Success) {
RepoSearchResult.Success var2 = (RepoSearchResult.Success)var1;
if (Intrinsics.areEqual(this.data, var2.data)) {
return true;
}
}
return false;
} else {
return true;
}
}
}
@元数据(
mv = {1, 4, 0},
bv = {1, 0, 3},
k = 1,
d1 = {"\u0000.\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0000\b\b\u0018\u00002\u00020\u0001B\u0011\u0012\n\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004¸\u0006\u0002\u0010\u0005J\r\u0010\b\u001a\u00060\u0003j\u0002`\u0004HÇ\u0003J\u0017\u0010\t\u001a\u00020\u00002\f\b\u0002\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004HÇ\u0001J\u0013\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rHÖ\u0003J\t\u0010\u000e\u001a\u00020\u000fHÖ
<details>
<summary>英文:</summary>
Decompiling that class to Java with IntelliJ produces this. I changed `Repo` to `String` so that it would compile locally without dependencies.
```java
@Metadata(
mv = {1, 4, 0},
bv = {1, 0, 3},
k = 1,
d1 = {"\u0000\u0016\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b6\u0018\u00002\u00020\u0001:\u0002\u0003\u0004B\u0007\b\u0002¢\u0006\u0002\u0010\u0002\u0082\u0001\u0002\u0005\u0006¨\u0006\u0007"},
d2 = {"LRepoSearchResult;", "", "()V", "Error", "Success", "LRepoSearchResult$Success;", "LRepoSearchResult$Error;", "test_kotlin"}
)
public abstract class RepoSearchResult {
private RepoSearchResult() {
}
// $FF: synthetic method
public RepoSearchResult(DefaultConstructorMarker $constructor_marker) {
this();
}
@Metadata(
mv = {1, 4, 0},
bv = {1, 0, 3},
k = 1,
d1 = {"\u0000*\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010 \n\u0002\u0010\u000e\n\u0002\b\u0006\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0002\b\u0002\b\u0086\b\u0018\u00002\u00020\u0001B\u0013\u0012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005J\u000f\u0010\b\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003HÆ\u0003J\u0019\u0010\t\u001a\u00020\u00002\u000e\b\u0002\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003HÆ\u0001J\u0013\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rHÖ\u0003J\t\u0010\u000e\u001a\u00020\u000fHÖ\u0001J\t\u0010\u0010\u001a\u00020\u0004HÖ\u0001R\u0017\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007¨\u0006\u0011"},
d2 = {"LRepoSearchResult$Success;", "LRepoSearchResult;", "data", "", "", "(Ljava/util/List;)V", "getData", "()Ljava/util/List;", "component1", "copy", "equals", "", "other", "", "hashCode", "", "toString", "test_kotlin"}
)
public static final class Success extends RepoSearchResult {
@NotNull
private final List data;
@NotNull
public final List getData() {
return this.data;
}
public Success(@NotNull List data) {
Intrinsics.checkParameterIsNotNull(data, "data");
super((DefaultConstructorMarker)null);
this.data = data;
}
@NotNull
public final List component1() {
return this.data;
}
@NotNull
public final RepoSearchResult.Success copy(@NotNull List data) {
Intrinsics.checkParameterIsNotNull(data, "data");
return new RepoSearchResult.Success(data);
}
// $FF: synthetic method
public static RepoSearchResult.Success copy$default(RepoSearchResult.Success var0, List var1, int var2, Object var3) {
if ((var2 & 1) != 0) {
var1 = var0.data;
}
return var0.copy(var1);
}
@NotNull
public String toString() {
return "Success(data=" + this.data + ")";
}
public int hashCode() {
List var10000 = this.data;
return var10000 != null ? var10000.hashCode() : 0;
}
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof RepoSearchResult.Success) {
RepoSearchResult.Success var2 = (RepoSearchResult.Success)var1;
if (Intrinsics.areEqual(this.data, var2.data)) {
return true;
}
}
return false;
} else {
return true;
}
}
}
@Metadata(
mv = {1, 4, 0},
bv = {1, 0, 3},
k = 1,
d1 = {"\u0000.\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0000\b\u0086\b\u0018\u00002\u00020\u0001B\u0011\u0012\n\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004¢\u0006\u0002\u0010\u0005J\r\u0010\b\u001a\u00060\u0003j\u0002`\u0004HÆ\u0003J\u0017\u0010\t\u001a\u00020\u00002\f\b\u0002\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004HÆ\u0001J\u0013\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rHÖ\u0003J\t\u0010\u000e\u001a\u00020\u000fHÖ\u0001J\t\u0010\u0010\u001a\u00020\u0011HÖ\u0001R\u0015\u0010\u0002\u001a\u00060\u0003j\u0002`\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0006\u0010\u0007¨\u0006\u0012"},
d2 = {"LRepoSearchResult$Error;", "LRepoSearchResult;", "error", "Ljava/lang/Exception;", "Lkotlin/Exception;", "(Ljava/lang/Exception;)V", "getError", "()Ljava/lang/Exception;", "component1", "copy", "equals", "", "other", "", "hashCode", "", "toString", "", "test_kotlin"}
)
public static final class Error extends RepoSearchResult {
@NotNull
private final Exception error;
@NotNull
public final Exception getError() {
return this.error;
}
public Error(@NotNull Exception error) {
Intrinsics.checkParameterIsNotNull(error, "error");
super((DefaultConstructorMarker)null);
this.error = error;
}
@NotNull
public final Exception component1() {
return this.error;
}
@NotNull
public final RepoSearchResult.Error copy(@NotNull Exception error) {
Intrinsics.checkParameterIsNotNull(error, "error");
return new RepoSearchResult.Error(error);
}
// $FF: synthetic method
public static RepoSearchResult.Error copy$default(RepoSearchResult.Error var0, Exception var1, int var2, Object var3) {
if ((var2 & 1) != 0) {
var1 = var0.error;
}
return var0.copy(var1);
}
@NotNull
public String toString() {
return "Error(error=" + this.error + ")";
}
public int hashCode() {
Exception var10000 = this.error;
return var10000 != null ? var10000.hashCode() : 0;
}
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof RepoSearchResult.Error) {
RepoSearchResult.Error var2 = (RepoSearchResult.Error)var1;
if (Intrinsics.areEqual(this.error, var2.error)) {
return true;
}
}
return false;
} else {
return true;
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论