普通 Class
kotlin
class TestClz {
val name: String = "hsw"
val age: Int = 18
}
java
public final class TestClz {
@NotNull
private final String name = "hsw";
private final int age = 18;
@NotNull
public final String getName() {
return this.name;
}
public final int getAge() {
return this.age;
}
}
转换为Java后的变化
- 类被
final
修饰,不可继承;加上open
可以去掉final
var
修饰的属性添加了get
set
方法 ,val
只有·get
方法
data class
kotlin
data class TestClz(val name: String, var age: Int)
java
public final class TestClz {
@NotNull
private final String name;
private int age;
@NotNull
public final String getName() {
return this.name;
}
public final int getAge() {
return this.age;
}
public final void setAge(int var1) {
this.age = var1;
}
public TestClz(@NotNull String name, int age) {
Intrinsics.checkNotNullParameter(name, "name");
super();
this.name = name;
this.age = age;
}
@NotNull
public final String component1() {
return this.name;
}
public final int component2() {
return this.age;
}
@NotNull
public final TestClz copy(@NotNull String name, int age) {
Intrinsics.checkNotNullParameter(name, "name");
return new TestClz(name, age);
}
// $FF: synthetic method
public static TestClz copy$default(TestClz var0, String var1, int var2, int var3, Object var4) {
if ((var3 & 1) != 0) {
var1 = var0.name;
}
if ((var3 & 2) != 0) {
var2 = var0.age;
}
return var0.copy(var1, var2);
}
@NotNull
public String toString() {
return "TestClz(name=" + this.name + ", age=" + this.age + ")";
}
public int hashCode() {
String var10000 = this.name;
return (var10000 != null ? var10000.hashCode() : 0) * 31 + Integer.hashCode(this.age);
}
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof TestClz) {
TestClz var2 = (TestClz)var1;
if (Intrinsics.areEqual(this.name, var2.name) && this.age == var2.age) {
return true;
}
}
return false;
} else {
return true;
}
}
}
转换为Java后的变化
- 类被
final
修饰,不可继承;加上open
可以去掉final
var
修饰的属性添加了get
set
方法 ,val
只有·get
方法- 实现了
toString
equal
hashCode
方法 - 类似于java bean
object
object TestClz {
val name: String = "hsw"
var age: Int = 18
fun getInfo(): String {
return "${name}-${age}"
}
}
public final class TestClz {
@NotNull
private static final String name;
private static int age;
@NotNull
public static final TestClz INSTANCE;
@NotNull
public final String getName() {
return name;
}
public final int getAge() {
return age;
}
public final void setAge(int var1) {
age = var1;
}
@NotNull
public final String getInfo() {
return name + '-' + age;
}
private TestClz() {
}
static {
TestClz var0 = new TestClz();
INSTANCE = var0;
name = "hsw";
age = 18;
}
}
转换为Java后的变化
- 类被
final
修饰,不可继承;加上open
可以去掉final
var
修饰的属性添加了get
set
方法 ,val
只有·get
方法- 饿汉式实现了单例
- java 不可直接调用
get
set
需要通过INSTANCE
, 属性和方法 加上@JvmStatic
可以转换为静态属性/方法 直接调用
内部类
class TestClz {
val name: String = "hsw"
companion object {
val grade = 12L
fun getInfo() {
println("name-$name")
}
}
object CCS {
val grade = 12L
fun getSome() {
println("name-$name")
}
}
class Info {
val grade = 12L
fun getSome() {
println("name-$name")
}
}
inner class WC {
val grade = 12L
fun getSome() {
println("name-${name}")
}
}
}
public final class TestClz {
@NotNull
private final String name = "hsw";
private static final long grade = 12L;
@NotNull
public static final Companion Companion = new Companion((DefaultConstructorMarker)null);
@NotNull
public final String getName() {
return this.name;
}
// companion 伴生对象
// kotlin 可直接访问属性和方法
// java 需要通过 Companion 对象访问
public static final class Companion {
public final long getGrade() {
return TestClz.grade;
}
public final void getInfo() {
}
private Companion() {
}
// $FF: synthetic method
public Companion(DefaultConstructorMarker $constructor_marker) {
this();
}
}
// object 相当于通过单例实现静态内部类对象
public static final class CCS {
private static final long grade;
@NotNull
public static final CCS INSTANCE;
public final long getGrade() {
return grade;
}
public final void getSome() {
}
private CCS() {
}
static {
CCS var0 = new CCS();
INSTANCE = var0;
grade = 12L;
}
}
// 普通 class 相当于静态内部类,需要实例化调用方法
public static final class Info {
private final long grade = 12L;
public final long getGrade() {
return this.grade;
}
public final void getSome() {
}
}
// inner class 内部类,可以直接使用外部类的属性和方法
public final class WC {
private final long grade = 12L;
public final long getGrade() {
return this.grade;
}
public final void getSome() {
String var1 = "name-" + TestClz.this.getName();
System.out.println(var1);
}
}
)
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » Kotlin Class
发表评论 取消回复