Kotlin快速上手篇之Android&Kotlin常用开发对比
Posted 思忆(GeorgeQin)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kotlin快速上手篇之Android&Kotlin常用开发对比相关的知识,希望对你有一定的参考价值。
变量定义与常用数据类型
java:
int i = 1;
long l = 2;
boolean b = true;
float f = 0;
double d = 0;
char c = 'A';
String s = "text";
Intent intent = new Intent();
Kotlin:
var i : Int = 1
var l : Long = 2
var b : Boolean = true
var f : Float = 0F
var d : Double = 0.0
var c : Char = 'A'
var s : String = "text"
var intent = Intent() //不写也可自动推导
常量定义
java:
final String text = "";
Kotlin:
val text = ""
静态常量定义
java:
static final String TEST = "";
Kotlin:
const val TEST = ""
或
@JvmField
val TEST=""
类和继承
java:
MainActivity.class
Kotlin:
MainActivity::class.java
java
public class MainActivity extends AppCompatActivity
kotlin
class MainActivity : AppCompatActivity()
类与及相关属性的声明
java
public class Order
private long OrderId;
public long getOrderId()
return OrderId;
public void setOrderId(long orderId)
OrderId = orderId;
kotlin
class Order
//不想生成get set方法用val
var orderId: Long? = null
//私有化set 如果需要的话
// private set
抽象类和接口
Java
public abstract class BaseActivity extends AppCompatActivity
abstract void test();
Kotlin
abstract class BaseActivity : AppCompatActivity()
abstract fun test()
java
public interface Callback
void onOk();
void onFail();
kotlin
interface Callback
fun onOk()
fun onFail()
//可加入默认声明
fun test()
kotlin.io.println (111)
构造方法
java
public class MyView extends View
public MyView(Context context)
this(context, null);
public MyView(Context context, @Nullable AttributeSet attrs)
this(context, attrs, 0);
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
kotlin
class MyView : View
constructor(context : Context) : this(context, null)
constructor(context : Context, attrs : AttributeSet?) : this(context, attrs, 0)
constructor(context : Context, attrs : AttributeSet?, defStyleAttr : Int) : super(context, attrs, defStyleAttr)
内部类:
Java
public class MainActivity extends AppCompatActivity
public class MyTask
Kotlin
class MainActivity : AppCompatActivity()
inner class MyTask
方法与重写:
java
public void test(String message)
kotlin
如果是Unit也可忽略不写
fun test(message : String) : Unit
//还可以添加默认值
fun test(message : String="默认值")
java
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
Kotlin
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
可变参数
java
public int add(int... array)
。。。
Kotlin
fun add(vararg array: Int) : Int
。。。。
静态方法:
java
public class Toasts
public static void show()
kotlin
jvmstatic
object Toasts
@JvmStatic
fun show()
或 伴生对象
class Toasts
companion object
fun show()
类型检测
java
if ("" instanceof String)
kotlin
if ("" is String)
强制转换
java
List<Object> list = new ArrayList<>();
String data = (String) list.get(0);
kotlin
var list = ArrayList<Any>()
var data:String = list[0] as String
或
var data:String = list[0] ?as String
模板语法:
java
int number = 100;
System.out.println(String.format("测试样本是%d", number));
kotlin
var number = 100
//可在括号中添加表达式
println("测试样本是$number")
或
println("测试样本是$number")
比较:
kotlin | java |
---|---|
a==b | a.equals(b) |
a===b | a==b |
循环
Java
String[] array = "1", "2", "3";
for (int i = 1; i < array.length; i++)
System.out.println(array[i]);
或
for (String text : array)
System.out.println(text);
Kotlin
val array = arrayListOf("1", "2", "3")
for (i in 1..array.size - 1)
println(array[i])
或
for (i in 1 until array.size)
println(array[i])
或
for (text in array)
println(text)
判断:
Java
int count = 1;
switch (count)
case 0:
System.out.println(count);
break;
case 1:
case 2:
System.out.println(count);
break;
default:
System.out.println(count);
break;
Kotlin
var count = 1
when (count)
0 ->
println(count)
in 1..2 ->
println(count)
else ->
println(count)
枚举
java
public enum OrderStatus
ACCEPT,
FETCH,
DISPATCH,
FINISH
kotlin
enum class OrderStatus
ACCEPT,
FETCH,
DISPATCH,
FINISH
或密闭类(超级枚举)
sealed class OrderStatus
object ACCEPT : OrderStatus()
object FETCH : OrderStatus();
object DISPATCH : OrderStatus()
object FINISH : OrderStatus();
以上是关于Kotlin快速上手篇之Android&Kotlin常用开发对比的主要内容,如果未能解决你的问题,请参考以下文章