Kotlin java-freestyle

华烈
2023-12-01

PackName

类名重复的现象尤为常见,故引用导入必然会有纷争

  • Java
import com.my.view.Card;

public class TestJava {

    private Card mViewCard;
    Private com.my.ui.Card mUiCard;
}
  • Kotlin
import com.my.view.Card
import com.my.ui.Card as uiCard

class TestKotlin {

    var mViewCard: Card ?= null
    var mUiCard: uiCard ?= null
}

Funtion

run

用 Java 惯用套路,为 kotlin 的类文件创建 main 执行函数,并设置好执行事项。然后欣然地右击下鼠标,尴尬… 没有 Run 命令

  • Java
package com.my;

public class TestJava {

    public static void main(String[] args) {
        out("this is a main fun!!!");
    }

    public static void out(String str) {
        System.out.println(str);
    }
}
  • Kotlin
package com.my

fun main(args: Array<String>) {
    TestKotlin().out("this is a main fun!!!")
}

class TestKotlin {

    fun out(str: String) {
        print(str)
    }
}

for

  • Java
for(int index = 0, k = list.size(); index < k, i+=2){// 递增循环}
for(int index = list.size(); index > 0, i-=2){// 递减循环}
  • Kotlin
for (index in 0..list.size step 2) {// 进步递增循环}
for (index in list.size downto 0 step 2) {// 反向进步递减循环}

Object

onlyRead

这里应该没啥难理解的地方,飘过~ var 定义可变属性,val 只读属性

  • Java
final String mName = "Kotlin";
  • Kotlin
val mName = "Kotlin"

null

前方高能,来一发 Kotlin 的空大,obj?.a?.b?.c?.d!!.e 哈哈哈

  • Java
String mName = null;

if (null != mName) {
    System.out.print(mName.length());
} else {
    System.out.print("mName is null");
}
  • Kotlin
var mName : String? = null

print(mName?.length?: "mName is null")

type

高能再现,又是一发 Kotlin 的空大,as? , as 傻傻分不清楚。应该是可以更简练的,对没错

  • Java
String myName = userInfo instanceof UserInfo ?
        ((UserInfo) userInfo).mName : "who are you";
  • Kotlin
var myName: String? = if (null != userInfo as? UserInfoKotlin) // 能不能
            (userInfo as UserInfoKotlin).mName else "who are you"

var myNick: String? = if (userInfo is UserInfoKotlin)// 是不是
            userInfo.mNick else "who are you"

copy

  • Java
class UserInfo{
    public String mName;
    public String mAge;

    public UserInfo(String name, String age){
        mName = name;
        mAge = age;
    }
}

UserInfo mUser1 = new UserInfo("Jome", 22);
UserInfo mUser2 = new UserInfo("Joke", mUser1.mAge)
  • Kotlin
data class UserInfo(val mName: String, val mAge: Int)

val mUser1 = UserInfo("Jome", 22)
val mUser2 = mUser1.copy(mName="Joke")

Character

  • Java
abstract class Source<T extends String, K> {

    abstract T dispatch();

    abstract Integer compiler(K other);
}

public class Test {
    public void foo(Source<String, Number> sr){
        Source<String, Number> x = sr;
            x.compiler(0.1f);
    }
}
  • Kotlin
abstract class Source<out T, in K> {
    abstract fun dispatch(): T
    abstract fun compiler(other: K): Int
}

fun test(sr: Source<String, Number>) {
    val x: Source<Any, Float> = sr
    x.compiler(1.0f)
}

Template

  • Java
UserInfo info = new UserInfo("张三");
String tag = "用户名:";
System.out.println(tag + info.mName);
  • Kotlin
var info= UserInfo("张三")
val tag = "用户名:"
print("测试$tag${info.mName}")

Annotation

  • Java
public @interface Ann {
    int intValue();
    String stringValue();
}
@Ann(intValue = 1, stringValue = "Java")
public class Test{

}
  • Kotlin
annotation class Ann(val intValue: Int, val stringValue: String) 
@Ann(intValue = 1, stringValue = "Kotlin") class Test{

}

 类似资料:

相关阅读

相关文章

相关问答