前言
目前项目使用java+kotlin的模式开发,作为一个android开发人员,如果你没有使用过kotlin那么就out了。kotlin被称之为 Android 世界的Swift,由 JetBrains 设计开发并开源.在Google I/O 2017中,Google 宣布 Kotlin 成为 Android 官方开发语言,所以其性能肯定不比Java差,与此同时,作为一个程序员选择使用kotlin作为android开发语言,将在一定程序上大大减少代码量,提升工作效率。下面我们就举点例子来看看它有多牛。
数据类
平时做json数据解析的时候,我们要写大量的数据类,一堆的get和set方法虽然编译器能自动帮我们生成,但我还是不想看到它们。kotlin帮我们实现了愿望,这些事情它都帮我们做了,看看就知道。
java实现方法
public class QueryRoadInfo {
private int totalNum; //泊位总数
private int userNum; //剩余泊位数
private double receivableMoney; //应收金额
private double porderMoney; //实收金额
private double preMoney; //预付金额
private double arrearageMoney; //欠费金额
private int arrearageCount; //欠费订单数量
private double recoverMoney; //追缴费用
private int recoverCount; //追缴订单数量
private double receivedMoney; //待收金额
private int blackCount; //黑名单数目
private int leaseCount; //租赁数
public int getLeaseCount() {
return leaseCount;
}
public void setLeaseCount(int leaseCount) {
this.leaseCount = leaseCount;
}
public int getBlackCount() {
return blackCount;
}
public void setBlackCount(int blackCount) {
this.blackCount = blackCount;
}
public void setTotalNum(int totalNum)
{
this.totalNum = totalNum;
}
public int getTotalNum()
{
return totalNum;
}
public void setUserNum(int userNum)
{
this.userNum = userNum;
}
public int getUserNum()
{
return userNum;
}
public double getPreMoney() {
return preMoney;
}
public void setPreMoney(double preMoney) {
this.preMoney = preMoney;
}
public double getArrearageMoney() {
return arrearageMoney;
}
public void setArrearageMoney(double arrearageMoney) {
this.arrearageMoney = arrearageMoney;
}
public int getArrearageCount() {
return arrearageCount;
}
public void setArrearageCount(int arrearageCount) {
this.arrearageCount = arrearageCount;
}
public double getRecoverMoney() {
return recoverMoney;
}
public void setRecoverMoney(double recoverMoney) {
this.recoverMoney = recoverMoney;
}
public int getRecoverCount() {
return recoverCount;
}
public void setRecoverCount(int recoverCount) {
this.recoverCount = recoverCount;
}
public double getReceivedMoney() {
return receivedMoney;
}
public void setReceivedMoney(double receivedMoney) {
this.receivedMoney = receivedMoney;
}
public double getReceivableMoney() {
return receivableMoney;
}
public void setReceivableMoney(double receivableMoney) {
this.receivableMoney = receivableMoney;
}
public double getPorderMoney() {
return porderMoney;
}
public void setPorderMoney(double porderMoney) {
this.porderMoney = porderMoney;
}
}
kotlin实现方法
data class QueryRoadInfo (var name:Int,var userNum:Int,var receivableMoney:Double,var porderMoney:Double,var preMoney:Double,var arrearageMoney:Double,
var arrearageCount:Int,var recoverMoney:Double,var recoverCount:Int,var receivedMoney:Double,var blackCount: Int,var leaseCount:Int)
是不是很简单,kotlin真的是 1行代码能顶java 100+
布局文件里面的控件可以直接调用
我们都知道,在写java代码的时候,要用到布局文件里面属性,得先通过id找到它们。但kotlin不用,可以通过在kotlin文件里面将布局文件import方式导入,就可以直接用它们。
新建activity_about.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/layout_title"/>
<TextView
android:id="@+id/info_version"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/about_us_detail"
android:textSize="16sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:background="@color/white"/>
</LinearLayout>
接着我们在AboutActivity调用里面的info_version,看到没有,IDE会自动提示可用的属性列表,这时候我们选择info_version,就会自动帮我们生成导入xml文件语句import kotlinx.android.synthetic.main.activity_about.*
我这只是列出其中两点,其实kotlin它的特性就说明了它的简约性,比如空安全,可扩展函数都大大给我们节约了代码量,并且它作为android的官方开发语言,我们还有什么理由不用它?