1.java中有一个常用的类是javabean,一般情况下需要有一个无参的构造函数,并且里面的参数不能用final来修饰,我们新建一个类作为例子:
public class GameBean {
int gameId;
String gameName;
public int getGameId() {
return gameId;
}
public void setGameId(int gameId) {
this.gameId = gameId;
}
public String getGameName() {
return gameName;
}
public void setGameName(String gameName) {
this.gameName = gameName;
}
public GameBean() {
}
}
2.在kotlin中呢,很简单我们这样写这个类
class GameBeanKotlin(var gid: Int, var gname: String)
3.在kotlin中的使用
fun main(args: Array<String>) {
val myGame=GameBeanKotlin(1,"吃鸡")
println(myGame)// 运行结果 GameBeanKotlin(gid=1, gname=吃鸡)
println(myGame.component1())// 运行结果 1
println(myGame.component2())//运行结果 吃鸡
}
4.在kotlin中运行时没有问题的:我们看一下kotlin的字符码
@Metadata(
mv = {1, 1, 16},
bv = {1, 0, 3},
k = 1,
d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0002\b\r\n\u0002\u0010\u000b\n\u0002\b\u0004\b\u0086\b\u0018\u00002\u00020\u0001B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006J\t\u0010\u000f\u001a\u00020\u0003HÆ\u0003J\t\u0010\u0010\u001a\u00020\u0005HÆ\u0003J\u001d\u0010\u0011\u001a\u00020\u00002\b\b\u0002\u0010\u0002\u001a\u00020\u00032\b\b\u0002\u0010\u0004\u001a\u00020\u0005HÆ\u0001J\u0013\u0010\u0012\u001a\u00020\u00132\b\u0010\u0014\u001a\u0004\u0018\u00010\u0001HÖ\u0003J\t\u0010\u0015\u001a\u00020\u0003HÖ\u0001J\t\u0010\u0016\u001a\u00020\u0005HÖ\u0001R\u001a\u0010\u0002\u001a\u00020\u0003X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0007\u0010\b\"\u0004\b\t\u0010\nR\u001a\u0010\u0004\u001a\u00020\u0005X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u000b\u0010\f\"\u0004\b\r\u0010\u000e¨\u0006\u0017"},
d2 = {"Lcom/stone/mykotlin/seven/GameBeanKotlin;", "", "gid", "", "gname", "", "(ILjava/lang/String;)V", "getGid", "()I", "setGid", "(I)V", "getGname", "()Ljava/lang/String;", "setGname", "(Ljava/lang/String;)V", "component1", "component2", "copy", "equals", "", "other", "hashCode", "toString", "app_debug"}
)
public final class GameBeanKotlin {
private int gid;
@NotNull
private String gname;
public final int getGid() {
return this.gid;
}
public final void setGid(int var1) {
this.gid = var1;
}
@NotNull
public final String getGname() {
return this.gname;
}
public final void setGname(@NotNull String var1) {
Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
this.gname = var1;
}
public GameBeanKotlin(int gid, @NotNull String gname) {
Intrinsics.checkParameterIsNotNull(gname, "gname");
super();
this.gid = gid;
this.gname = gname;
}
public final int component1() {
return this.gid;
}
@NotNull
public final String component2() {
return this.gname;
}
@NotNull
public final GameBeanKotlin copy(int gid, @NotNull String gname) {
Intrinsics.checkParameterIsNotNull(gname, "gname");
return new GameBeanKotlin(gid, gname);
}
// $FF: synthetic method
public static GameBeanKotlin copy$default(GameBeanKotlin var0, int var1, String var2, int var3, Object var4) {
if ((var3 & 1) != 0) {
var1 = var0.gid;
}
if ((var3 & 2) != 0) {
var2 = var0.gname;
}
return var0.copy(var1, var2);
}
@NotNull
public String toString() {
return "GameBeanKotlin(gid=" + this.gid + ", gname=" + this.gname + ")";
}
public int hashCode() {
int var10000 = this.gid * 31;
String var10001 = this.gname;
return var10000 + (var10001 != null ? var10001.hashCode() : 0);
}
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof GameBeanKotlin) {
GameBeanKotlin var2 = (GameBeanKotlin)var1;
if (this.gid == var2.gid && Intrinsics.areEqual(this.gname, var2.gname)) {
return true;
}
}
return false;
} else {
return true;
}
}
}
// GameKotlinKt.java
package com.stone.mykotlin.seven;
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {1, 1, 16},
bv = {1, 0, 3},
k = 2,
d1 = {"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0002\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005¨\u0006\u0006"},
d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "app_debug"}
)
public final class GameKotlinKt {
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
GameBeanKotlin myGame = new GameBeanKotlin(1, "吃鸡");
boolean var2 = false;
System.out.println(myGame);
int var4 = myGame.component1();
boolean var3 = false;
System.out.println(var4);
String var5 = myGame.component2();
var3 = false;
System.out.println(var5);
}
}
可以看到类和get以及set方法前面都一个final修饰,不可以被继承,所以我们需要安装官方提供的两个插件noArg和allOpen来解决这个问题
5.项目跟目录的build.gradle
buildscript {
ext.kotlin_version = '1.3.72'
ext.butterknife_version='10.2.1'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.jakewharton:butterknife-gradle-plugin:$butterknife_version"
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
6.app的 build.gradle文件
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
//下面这两行才是
apply plugin: 'kotlin-noarg'
apply plugin: 'kotlin-allopen'
7.在app的项目里面新建一个文件(或者是新的包),名字随意定义
noargallopen.png8.app的 build.gradle文件中
android {
...
noArg {
annotation("com.stone.mykotlin.anNoArgAllOpen")
}
allOpen {
annotation("com.stone.mykotlin.anNoArgAllOpen")
}
}
9,sync now
10.在kotlin的bean类上
@NoArgAllOpen
data class GameBeanKotlin(var gid: Int, var gname: String)
11.clean 项目,重新查看kotlin字符码
@NoargAllopen
@Metadata(
mv = {1, 1, 16},
bv = {1, 0, 3},
k = 1,
d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0002\b\r\n\u0002\u0010\u000b\n\u0002\b\u0004\b\u0097\b\u0018\u00002\u00020\u0001B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006J\t\u0010\u000f\u001a\u00020\u0003HÆ\u0003J\t\u0010\u0010\u001a\u00020\u0005HÆ\u0003J\u001d\u0010\u0011\u001a\u00020\u00002\b\b\u0002\u0010\u0002\u001a\u00020\u00032\b\b\u0002\u0010\u0004\u001a\u00020\u0005HÆ\u0001J\u0013\u0010\u0012\u001a\u00020\u00132\b\u0010\u0014\u001a\u0004\u0018\u00010\u0001HÖ\u0003J\t\u0010\u0015\u001a\u00020\u0003HÖ\u0001J\t\u0010\u0016\u001a\u00020\u0005HÖ\u0001R\u001a\u0010\u0002\u001a\u00020\u0003X\u0096\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0007\u0010\b\"\u0004\b\t\u0010\nR\u001a\u0010\u0004\u001a\u00020\u0005X\u0096\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u000b\u0010\f\"\u0004\b\r\u0010\u000e¨\u0006\u0017"},
d2 = {"Lcom/stone/mykotlin/seven/GameBeanKotlin;", "", "gid", "", "gname", "", "(ILjava/lang/String;)V", "getGid", "()I", "setGid", "(I)V", "getGname", "()Ljava/lang/String;", "setGname", "(Ljava/lang/String;)V", "component1", "component2", "copy", "equals", "", "other", "hashCode", "toString", "app_debug"}
)
public class GameBeanKotlin {
private int gid;
@NotNull
private String gname;
public int getGid() {
return this.gid;
}
public void setGid(int var1) {
this.gid = var1;
}
@NotNull
public String getGname() {
return this.gname;
}
public void setGname(@NotNull String var1) {
Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
this.gname = var1;
}
public GameBeanKotlin(int gid, @NotNull String gname) {
Intrinsics.checkParameterIsNotNull(gname, "gname");
super();
this.gid = gid;
this.gname = gname;
}
public final int component1() {
return this.getGid();
}
@NotNull
public final String component2() {
return this.getGname();
}
@NotNull
public final GameBeanKotlin copy(int gid, @NotNull String gname) {
Intrinsics.checkParameterIsNotNull(gname, "gname");
return new GameBeanKotlin(gid, gname);
}
// $FF: synthetic method
public static GameBeanKotlin copy$default(GameBeanKotlin var0, int var1, String var2, int var3, Object var4) {
if (var4 != null) {
throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: copy");
} else {
if ((var3 & 1) != 0) {
var1 = var0.getGid();
}
if ((var3 & 2) != 0) {
var2 = var0.getGname();
}
return var0.copy(var1, var2);
}
}
@NotNull
public String toString() {
return "GameBeanKotlin(gid=" + this.getGid() + ", gname=" + this.getGname() + ")";
}
public int hashCode() {
int var10000 = this.getGid() * 31;
String var10001 = this.getGname();
return var10000 + (var10001 != null ? var10001.hashCode() : 0);
}
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof GameBeanKotlin) {
GameBeanKotlin var2 = (GameBeanKotlin)var1;
if (this.getGid() == var2.getGid() && Intrinsics.areEqual(this.getGname(), var2.getGname())) {
return true;
}
}
return false;
} else {
return true;
}
}
public GameBeanKotlin() {
}
}
// GameKotlinKt.java
package com.stone.mykotlin.seven;
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
@Metadata(
mv = {1, 1, 16},
bv = {1, 0, 3},
k = 2,
d1 = {"\u0000\u0014\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0002\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005¨\u0006\u0006"},
d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "app_debug"}
)
public final class GameKotlinKt {
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
GameBeanKotlin myGame = new GameBeanKotlin(1, "吃鸡");
boolean var2 = false;
System.out.println(myGame);
int var4 = myGame.component1();
boolean var3 = false;
System.out.println(var4);
String var5 = myGame.component2();
var3 = false;
System.out.println(var5);
}
}
网友评论