1. 程式人生 > >Kotlin學習筆記2 : 變數常量與基本資料型別

Kotlin學習筆記2 : 變數常量與基本資料型別

一:繼承與類與方法定義:

(1)繼承用一個英文冒號: extends---> :

預設的類是不可以被繼承的,只能繼承宣告為open或者abstract的類

open class Person (name: String , surname: String) {
}

(2)建立類用class:如 class A{},

還可以帶引數,也可以寫方法體:

open class Person (name: String , surname: String){

    init {
        //編寫函式體

    }
}

//class Person(name: String , surname: String)

(3)方法定義用fun

    //若沒有方法型別(相當於java的void):預設返回Unit
    //(1)定義一個沒有引數,沒有返回型別的方法
    fun n1(){

    }
    //(2)定義一個有一個Int引數的無返回方法
    fun add(x: Int ){

    }
    //(3)定義一個返回int型別的方法:兩個引數
    fun add(x: Int , y: Int) : Int{
        return x + y; //分號可以省略的
    }

如上圖:

可以定義沒有返回型別的方法,如fun n1()

也可以定義有返回型別的方法,在方法後加冒號和返回型別,甚至返回值,如:fun n1() : Int{}

    //(4)定義直接返回的方法:(3)的改寫,與(3)一樣
    fun add2(x: Int , y: Int) : Int = x + y
還可以在引數中指定預設值,如:
    //第三個引數指定了一個預設值,呼叫時可以不傳
    fun toast(context: Context, message: String, length: Int= Toast.LENGTH_SHORT){
        Toast.makeText(context , message , length).show();
    }
    fun toast2(message: String , length: Int= Toast.LENGTH_SHORT){
        Toast.makeText(this , message , length).show();
    }

二,插入表示式'$’:

(1)當插入為簡單型別時如字串,可以直接加在$符號後面

(2)當插入為複雜型別時,person.name,則需要用花括號括起來再加在$符號後面

如下面所示:

    //$代表插入表示式,複雜則用{}括起來,簡單自己插入
    fun SayHello(message: String) : String{
        return "Your name is $message"
        //return "66 is ${Person.name}"
    }
Person是自己定義的bean類

三,val與var,以及各種基本型別

(1)val與var

val:只能賦值一次,不可以改變

  ---如:val i=1

              i=2    //編譯器會報錯,val是不可以改變的

var:可以多次賦值,可以改變的

(2)基本型別:數字(Numbers),字元(Characters),布林(Boolean),陣列(Array),字串(String)

數字:Byte,Short,Int,Long,Float,Double

幾個基本型別預設:

        val i = 1       //Int
        val i2 = 0x0f   //0x---16進位制,,6進位制Int
        val d = 3.5     //Double
        val f = 1.5f    //Float型別
        val l = 3L      //Long型別
字元與字串:
        val str = "Hello"
        val c = str[1]; //其實就是e
遍歷字串並列印:
        val str = "Hello"
        for(c in str){
            print(c)
        }

(3)相互轉化:

每個number型別和String支援如下的轉換
toByte(): Byte
toShort(): Short
toInt(): Int
toLong(): Long
toFloat(): Float
toDouble(): Double
toChar(): Char
Char型別支援
toInt(): Int
所有資料型別(不僅是基本資料型別)支援
toString(): String

如:

        val a : Int=7 //定義一個Int變數a,值為7
        val d : Double = a.toDouble()  //把a轉化為Double型別,賦給d
        Log.d("MainActivity" , "a= " + a )
        Log.d("MainActivity" , "d= " + d )

結果:
05-28 02:54:04.594 5560-5560/? D/MainActivity: a= 7
05-28 02:54:04.594 5560-5560/? D/MainActivity: d= 7.0

四,一個簡單的使用RecyclerView的例子:

佈局:定義一個RecyclerView:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView

        android:id="@+id/forecast_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</FrameLayout>
Adapter類:
class ForecastListAdapter(val items: List<String>) :
        RecyclerView.Adapter<ForecastListAdapter.ViewHolder>() {

    /**
     * 初始化
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {


        return ViewHolder(TextView(parent.context))
    }

    /**
     * 為holder繫結id
     */
    override fun onBindViewHolder(holder: ViewHolder , position: Int) {

        holder.textView.text = items[position]
    }

    /**
     * 返回數目
     */
    override fun getItemCount(): Int {

        return items.size
    }


    /**
     * 建立一個內部的ViewHolder類:定義一個TextView
     */
    class ViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)

}
主類:
class MainActivity : AppCompatActivity() {

    //建立一個私有的不可變的List<T>型別
    private val items = listOf(
            "Mon 5/28 - Sunny - 31/17",
            "Tue 5/29 - Foggy - 21/9",
            "Wed 5/30 - Cloudy - 19/11",
            "Thurs 6/1 - Rainy - 27/25",
            "Fri 6/2 - Foggy - 29/27",
            "Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
            "Sun 6/4 - Sunny - 32/30",
            "Mon 5/28 - Sunny - 31/17",
            "Tue 5/29 - Foggy - 21/9",
            "Wed 5/30 - Cloudy - 19/11",
            "Thurs 6/1 - Rainy - 27/25",
            "Fri 6/2 - Foggy - 29/27",
            "Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
            "Sun 6/4 - Sunny - 32/30",
            "Mon 5/28 - Sunny - 31/17",
            "Tue 5/29 - Foggy - 21/9",
            "Wed 5/30 - Cloudy - 19/11",
            "Thurs 6/1 - Rainy - 27/25",
            "Fri 6/2 - Foggy - 29/27",
            "Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
            "Sun 6/4 - Sunny - 32/30",
            "Mon 5/28 - Sunny - 31/17",
            "Tue 5/29 - Foggy - 21/9",
            "Wed 5/30 - Cloudy - 19/11",
            "Thurs 6/1 - Rainy - 27/25",
            "Fri 6/2 - Foggy - 29/27",
            "Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
            "Sun 6/4 - Sunny - 32/30",
            "Mon 5/28 - Sunny - 31/17",
            "Tue 5/29 - Foggy - 21/9",
            "Wed 5/30 - Cloudy - 19/11",
            "Thurs 6/1 - Rainy - 27/25",
            "Fri 6/2 - Foggy - 29/27",
            "Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
            "Sun 6/4 - Sunny - 32/30",
            "Mon 5/28 - Sunny - 31/17",
            "Tue 5/29 - Foggy - 21/9",
            "Wed 5/30 - Cloudy - 19/11",
            "Thurs 6/1 - Rainy - 27/25",
            "Fri 6/2 - Foggy - 29/27",
            "Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
            "Sun 6/4 - Sunny - 32/30",
            "Mon 5/28 - Sunny - 31/17",
            "Tue 5/29 - Foggy - 21/9",
            "Wed 5/30 - Cloudy - 19/11",
            "Thurs 6/1 - Rainy - 27/25",
            "Fri 6/2 - Foggy - 29/27",
            "Sat 6/3 - TRAPPED IN WEATHERSTATION - 31/28",
            "Sun 6/4 - Sunny - 32/30"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.recycler_layout)

        //繫結控制元件id---recyclerView
        val forecastList = findViewById(R.id.forecast_list) as RecyclerView
        //為recyclerView建立LinearLayoutManager例項
        forecastList.layoutManager = LinearLayoutManager(this)

        //為recyclerView設定介面卡
        forecastList.adapter = ForecastListAdapter(items)

    }
}
結果:



一個可以下拉的recyclerView

五,總結幾點:

1,每個句子後面不需要新增分號---';'

2.定義引數時,型別在後邊,名字在前面,中間加冒號,如

    fun add(x: Int ){

    }

3,不需要用new

好了,其他的再說