先熟悉一下基本數據類型
類型 | 位寬度 |
---|---|
Double | 64 |
Float | 32 |
Long | 64 |
Int | 32 |
Short | 16 |
Byte | 8 |
1、類和屬性的定義
/**
* 類的定義及屬性
*/
open class A {
lateinit var name : String
var age : Int = 0
var gender : Int = 0
fun setInterface(i : I){
}
}
//定義int的方式一
var a : Int = 0
//定義int的方式二,同一
var b = 0
//定義一個字符串
var s = "ssssss"
//定義String,lateinit關鍵字 可以不初始化,但在使用前必須初始化,否則會空指針
lateinit var c : String
//加個問號,表名這個屬性可以爲空,在使用時需要在屬性名後加雙感嘆號,表示屬性不爲空時調用方法, 如:d!!.toString()
var d : String? = null
2、方法定義
/**
* 無參方法定義
*/
fun main(){
}
/**
* 有參方法定義
* args String數組定義
*/
fun main(args: Array<String>) {
}
/**
* 有返回值方法定義
* 返回字符串
*/
fun mains(): String {
return ""
}
/**
* 有默認參數方法的定義
*/
fun print(s : String = "沒有內容"){
}
//調用
//不傳參數,則s = "沒有內容"
print()
//傳參,則s = "我是內容"
print("我是內容")
3、部分具體語法
//list基本語法
val list = ArrayList<String>()
list.add("")
val s = list[0]
val size = list.size
//list遍歷
for(i in list.indices){
val s = list[i]
}
list.forEach {
val s = it
}
//普通循環
for(i in 0..9){
}
//switch...case..
var key = 1
when(key){
0->{
}
1->{
}
else->{
}
}
var a = 1
//類似三目運算符,kotlin沒有三目
var b = if (a > 0) 1 else 2
//類型轉換
//toByte(): Byte
//toShort(): Short
//toInt(): Int
//toLong(): Long
//toFloat(): Float
//toDouble(): Double
//toChar(): Char
val b: Byte = 1 // OK, 字面值是靜態檢測的
val i: Int = b.toInt() // OK
4、接口的定義
/**
* 接口定義
*/
interface I {
fun a(string: String?)
}
var a = A()
a.setInterface(object : I{
override fun a(string: String?) {
}
})
5、繼承和實現
/**
* 實現接口
*/
internal class Child : I {
override fun a(string: String?) {
}
}
/**
* 類的繼承,接口實現
*/
class B : A(),I{
override fun a(string: String?) {
}
}
class C : 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) {}
}
6、工具類定義(類似靜態方法)
/**
* 工具類,類似靜態方法
*/
object Utils {
fun eat() {
}
}
//靜態方法調用
Utils.eat()
7、類的操作
//類的聲明及屬性的操作
var xiaoming = A()
xiaoming.age = 12
xiaoming.name = "xiaoming"
xiaoming.gender = 0
var age = xiaoming.age
var name = xiaoming.name
8、擴展函數
擴展函數可以在已有類中添加新的方法,不會對原類做修改,定義一個*.kt文件,如:在MainActivity中添加一個log()方法,內容如下:
fun MainActivity.log() {
}
這個很好用,例如log之類的,或者某些類專用且子類通用的方法。上面的定義只有在MainActivity及子類中可以調用到log方法
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//inline文件中聲明的擴展方法
log()
}
}
9、view的操作
MainActivity的佈局中有一個TextView id=tv
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
activity中不用findViewById,也不用聲明view的名稱,只需要導入這個佈局,就可以直接拿到id對象
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//setText
tv.text = "abcd"
}
}
監聽方法,以下兩種寫法是一樣的
tv.setOnClickListener(object : View.OnClickListener{
override fun onClick(view: View?) {
view.setBackgroundColor(Color.BLACK)
}
})
tv.setOnClickListener {
//上面的view在這裏是 it
it.setBackgroundColor(Color.BLACK)
}