Kotlin-簡約之美-進階篇(十八):與Java的較量

@[toc]
在Java當中一些常量通常情況下都是定義在接口當中,默認情況下所有的成員變量都是public static final類型的,所有的方法都是public abstract類型的,而在Kotlin中的接口是不允許定義常量的,但是我們可以通過伴生對象companion object來解決這個問題。

數(shù)據(jù)實體類

  • Java中的實現(xiàn)
public class NewsType {
    public int id;
    public String typename;
      public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTypename() {
        return typename;
    }
    public void setTypename(String typename) {
        this.typename = typename;
    }
  • Kotlin中的實現(xiàn)
class NewsType {
    var id: Int = 0
    var typename: String? = null
    var addtime: String? = null
}    

接口變量

  • Java中的定義:
public interface NewsDBConfig {
    String DB_NAME = "fly_news.db";
    int VERSION_CODE = 1;

    interface NewsType {
        String TABLE_NAME = "news_type";
        String CLUMN_ID = "id";
        String CLUMN_TYPE_NAME = "typename";
        String CLUMN_ADD_TIME = "addtime";
        String CREATE_TABLE = "create table " + TABLE_NAME + " ("+CLUMN_ID+" integer PRIMARY KEY autoincrement," + CLUMN_TYPE_NAME + " varchar(64),"+CLUMN_ADD_TIME+" datetime default (datetime('now','localtime')))";
    }
}
  • Kotlin中的定義
interface NewsDBConfig {

    interface NewsType {
        companion object {
            val TABLE_NAME = "news_type"
            val CLUMN_ID = "id"
            val CLUMN_TYPE_NAME = "typename"
            val CLUMN_ADD_TIME = "addtime"
            val CREATE_TABLE = "create table $TABLE_NAME ($CLUMN_ID integer PRIMARY KEY autoincrement,$CLUMN_TYPE_NAME varchar(64),$CLUMN_ADD_TIME datetime default (datetime('now','localtime')))"
        }
    }
    companion object {
        val DB_NAME = "fly_news.db"
        val VERSION_CODE = 1
    }
}

懶漢式單例

  • Java中的實現(xiàn)
public class NewsDBHelper extends SQLiteOpenHelper {
    private static NewsDBHelper mNewsDBHelper;
    public static NewsDBHelper getInstance(Context context) {
        if (mNewsDBHelper == null) {
            synchronized (NewsDBHelper.class) {
                mNewsDBHelper = new NewsDBHelper(context);
            }
        }
        return mNewsDBHelper;
    }

    private NewsDBHelper(Context context) {
        super(context, NewsDBConfig.DB_NAME, null, NewsDBConfig.VERSION_CODE);
    }
}

  • Kotlin中的實現(xiàn)
class NewsDBHelper private constructor(context: Context) :
    SQLiteOpenHelper(context, NewsDBConfig.DB_NAME, null, NewsDBConfig.VERSION_CODE) {

    companion object {
        private var mNewsDBHelper: NewsDBHelper? = null
        fun getInstance(context: Context): NewsDBHelper? {
            if (mNewsDBHelper == null) {
                synchronized(NewsDBHelper::class.java) {
                    if (mNewsDBHelper == null) {
                        mNewsDBHelper = NewsDBHelper(context)
                    }
                }
            }
            return mNewsDBHelper
        }
    }
}

構(gòu)造方法

情況1
  • Java中的實現(xiàn)
public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}
  • Kotlin中的實現(xiàn)
class MyView : View {
    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context,attrs,defStyleAttr) {}
}
情況2
  • java中的實現(xiàn)
public class MyView1 extends View {
    public MyView1(Context context) {
        this(context,null);
    }

    public MyView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}
  • Kotlin中的實現(xiàn)
class MyView1 @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr)

或者

class MyView2 : View {
    @JvmOverloads
    constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
    ) : super(context, attrs, defStyleAttr) {  }

lambda的應用

無參數(shù)
  1. 常規(guī)寫法
fun print() { 
    println("HelloWorld")
}
  1. lambda標準寫法
var print1: () -> Unit = {println("HelloWorld1")}
  1. lambda簡寫
var print2 = {println("HelloWorld2")}
有參數(shù)加法
  1. 常規(guī)寫法
fun add(x: Int, y: Int): Int {
    return x + y
}
  1. lambda標準寫法
var add1: (Int, Int) -> Int = { x, y -> x + y }
  1. lambda簡寫
var add2 = { x: Int, y: Int -> x + y }
作為函數(shù)參數(shù)
  1. 一般寫法
fun add3(x: Int, y: Int): Int {return x * y}
fun add4(a: Int, b: Int): Int {return a + b}
//調(diào)用
var result = add4(1, add3(2, 3))
  1. lambda寫法
fun add5(x:Int,add6:(Int,Int)->Int):Int{
    return x + add6.invoke(2,3)
}
//調(diào)用
var result5 = add5(1,{x:Int,y:Int -> x*y})
  1. lambda寫法
fun add7(x:Int,add8:(Int)->Boolean):Int{
    if(add8(x)){
        return x
    }else{
        return 0
    }
}
//調(diào)用1
var add81 = add7(10,{x:Int -> x > 0})
//調(diào)用2
var add82 = add7(10,{it > 0})

匿名函數(shù)

  1. 標準寫法
fun add(x: Int, y: Int): Int {
    return x + y
}

或者

fun add1(x: Int, y: Int): Int = x + y
  1. 匿名寫法
var add2 = fun(x: Int, y: Int): Int {
    return x + y
}
  1. 匿名寫法
var add3 = fun(x: Int, y: Int): Int = x + y

閉包

1.函數(shù)嵌套

fun test1() {
    println("test1")
    fun test2() {
        println("test2")
    }
}

2.函數(shù)作為返回值

fun test2(x: Int): () -> Int {
    var a = 0
    return fun(): Int {
        a++
        return a + x
    }
}
//調(diào)用
var test = test2(10)
println(test())
println(test())
println(test())

3.函數(shù)作為返回值

fun test3(v: Int): (Int, Int) -> Int {
    var a = 1
    var b = 2
    return { x, y -> x + y + a + b + v}
}
//調(diào)用
var test2 =test3(10)
var result = test2(12,1)
println(result)
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容