变量
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.静态变量
companion object {
var sText = "companion text";
}
函数
1.定义函数(或方法)
fun onClickTest(view: View) {
}
2.定义带返回值的函数
fun onClickTest(view: View) :String {
return "";
}
3.定义返回值可为空的函数
fun onClickTest(view: View) :String? {
return null;
}
类
Kotlin类文件名为*.kt,一个类可以有(0或1)个主构造函数或多个次构造函数
主构造函数:
i.可以将主构造函数中参数申明成类中成员变量
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;
}
}
次构造函数:
i.不能将次构造函数中参数申明成成员变量
如 以下是错误的
ii.如果有主构造函数,次构造函数必须要实现主构造函数,对象构造后会调用init 代码块
open class Food(name: String){
lateinit var mName:String;
init {
mName = name;
}
constructor(nameStr: String, weight: Int) : this(nameStr) {
}
}
iii.子类的要实现父类的主构造函数
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) {
}
内部类
Kotlin中内部类都是静态的,如以下内部类Food无法调用外部成员变量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) {
}
}
}
非静态内部类,需要用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 = [email protected];
}
}
}
接口
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 = [email protected];
var fileFilter: FileFilter = FileFilter { pathname: File -> var end = ".java"; pathname.absolutePath.endsWith(end) }
//实现kotlin中的接口,不能整成上面那种匿名内部类的方式,需采用下面的方法
var operation: Operation = object:Operation {
override fun add(a: Int, b: Int): Int {
return a+b
}
}
//kotlin中的抽象类也不能生成匿名内部类
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(声明函数类型别名)
typealias Operate = (a:T,b:T) -> T
fun init() {
var operate: (Int, Int) -> Int = { a:Int, b:Int ->a+b}
operate.invoke(1,2)
}