變量
1.常量
val text:String = "hell world!";
2.變量
var view:View = LayoutInflater.from(this).inflate(R.layout.dialog_kotlin,null, false);
3.可為空的變量
var message:String? = "this is a dialog message!";
4.可以不指定類型
var name: String? = null;
var testName = "test"
5.靜態(tài)變量
companion object {
var sText = "companion text";
}
函數(shù)
1.定義函數(shù)(或方法)
fun onClickTest(view: View) {
}
2.定義帶返回值的函數(shù)
fun onClickTest(view: View) :String {
return "";
}
3.定義返回值可為空的函數(shù)
fun onClickTest(view: View) :String? {
return null;
}
類
Kotlin類文件名為*.kt,一個類可以有(0或1)個主構(gòu)造函數(shù)或多個次構(gòu)造函數(shù)
主構(gòu)造函數(shù):
i.可以將主構(gòu)造函數(shù)中參數(shù)申明成類中成員變量
open class Animal(var nameStr: String) {
open fun test(){
val testName = nameStr;
}
}
ii.可以不作為成員變量
open class Food(name: String){
lateinit var mName:String;
init {
mName = name;
}
}
次構(gòu)造函數(shù):
i.不能將次構(gòu)造函數(shù)中參數(shù)申明成成員變量
如 以下是錯誤的
ii.如果有主構(gòu)造函數(shù)耍休,次構(gòu)造函數(shù)必須要實現(xiàn)主構(gòu)造函數(shù)痹愚,對象構(gòu)造后會調(diào)用init 代碼塊
open class Food(name: String){
lateinit var mName:String;
init {
mName = name;
}
constructor(nameStr: String, weight: Int) : this(nameStr) {
}
}
iii.子類的要實現(xiàn)父類的主構(gòu)造函數(shù)
open class Animal(var nameStr: String) {
open fun test(){
val testName = nameStr;
}
}
class Duck : Animal("鴨子") {
override fun toString(): String {
return "name:"+nameStr;
}
}
class Dog(var name: String) : Animal(nameStr = name) {
override fun test(){
val testName = nameStr;
this.name ="";
}
}
class Pig(name: String, weight: Int) : Animal(nameStr = name) {
}
內(nèi)部類
Kotlin中內(nèi)部類都是靜態(tài)的,如以下內(nèi)部類Food無法調(diào)用外部成員變量parentVar
class KotlinActivity : AppCompatActivity() {
var parentVar:String = "parent var";
open class Food(name: String){
lateinit var mName:String;
init {
mName = name;
mName = parentVar;
mName = KotlinActivity().parentVar;
}
constructor(nameStr: String, weight: Int) : this(nameStr) {
}
}
}
非靜態(tài)內(nèi)部類,需要用inner標示赖草,如下:
class GameActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game)
start.setOnClickListener { gameView.start() }
}
fun onScoreChanged(scoreValue: Int) {
score.text = scoreValue.toString()
}
override fun onPause() {
super.onPause()
}
inner class Test{
fun init(){
var context = this@GameActivity.applicationContext;
}
}
}
接口
package com.ifnoif.game2048
import android.app.Activity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_game.*
import java.io.File
import java.io.FileFilter
/**
* Created by shen on 17/4/11.
*/
class GameActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game)
start.setOnClickListener { gameView.start() }
}
fun onScoreChanged(scoreValue: Int) {
score.text = scoreValue.toString()
}
override fun onPause() {
super.onPause()
}
inner class Test{
fun init(){
var context = this@GameActivity.applicationContext;
var fileFilter: FileFilter = FileFilter { pathname: File -> var end = ".java"; pathname.absolutePath.endsWith(end) }
//實現(xiàn)kotlin中的接口滴某,不能整成上面那種匿名內(nèi)部類的方式,需采用下面的方法
var operation: Operation = object:Operation {
override fun add(a: Int, b: Int): Int {
return a+b
}
}
//kotlin中的抽象類也不能生成匿名內(nèi)部類
var add: Operation = object:Add() {
override fun add(a: Int, b: Int): Int {
return a+b
}
}
}
}
//抽象類
abstract class Add(): Operation{
}
//定義接口interface
open interface Operation {
fun add(a: Int, b: Int): Int;
}
}
還可以使用typealias(聲明函數(shù)類型別名)
typealias Operate<T> = (a:T,b:T) -> T
fun init() {
var operate: (Int, Int) -> Int = { a:Int, b:Int ->a+b}
operate.invoke(1,2)
}