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

好了,其他的再說


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章