类名重复的现象尤为常见,故引用导入必然会有纷争
import com.my.view.Card;
public class TestJava {
private Card mViewCard;
Private com.my.ui.Card mUiCard;
}
import com.my.view.Card
import com.my.ui.Card as uiCard
class TestKotlin {
var mViewCard: Card ?= null
var mUiCard: uiCard ?= null
}
用 Java 惯用套路,为 kotlin 的类文件创建 main 执行函数,并设置好执行事项。然后欣然地右击下鼠标,尴尬… 没有 Run 命令
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);
}
}
package com.my
fun main(args: Array<String>) {
TestKotlin().out("this is a main fun!!!")
}
class TestKotlin {
fun out(str: String) {
print(str)
}
}
for(int index = 0, k = list.size(); index < k, i+=2){// 递增循环}
for(int index = list.size(); index > 0, i-=2){// 递减循环}
for (index in 0..list.size step 2) {// 进步递增循环}
for (index in list.size downto 0 step 2) {// 反向进步递减循环}
这里应该没啥难理解的地方,飘过~ var 定义可变属性,val 只读属性
final String mName = "Kotlin";
val mName = "Kotlin"
前方高能,来一发 Kotlin 的空大,obj?.a?.b?.c?.d!!.e 哈哈哈
String mName = null;
if (null != mName) {
System.out.print(mName.length());
} else {
System.out.print("mName is null");
}
var mName : String? = null
print(mName?.length?: "mName is null")
高能再现,又是一发 Kotlin 的空大,as? , as 傻傻分不清楚。应该是可以更简练的,对没错
String myName = userInfo instanceof UserInfo ?
((UserInfo) userInfo).mName : "who are you";
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"
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)
data class UserInfo(val mName: String, val mAge: Int)
val mUser1 = UserInfo("Jome", 22)
val mUser2 = mUser1.copy(mName="Joke")
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);
}
}
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)
}
UserInfo info = new UserInfo("张三");
String tag = "用户名:";
System.out.println(tag + info.mName);
var info= UserInfo("张三")
val tag = "用户名:"
print("测试$tag${info.mName}")
public @interface Ann {
int intValue();
String stringValue();
}
@Ann(intValue = 1, stringValue = "Java")
public class Test{
}
annotation class Ann(val intValue: Int, val stringValue: String)
@Ann(intValue = 1, stringValue = "Kotlin") class Test{
}