Java和Kotlin对比 http://kotlinlang.org/docs/reference/java-interop.html
类和继承:http://kotlinlang.org/docs/reference/classes.html
类定义:class
class Invoice {
}
class Person{
}
class Person // 如果是一个空类,{}可以省略
构造函数:
只有一个主构造函数,一个或者多个次级构造函数。
class Person constructor(firstName: String) {
}
//如果构造函数没有任何注解或者修饰符 constructor可以省略
class Person(firstName: String) {
}
注意:主构造函数不能包含任何代码(定义函数和属性除外),可以通过init{}函数进行操作
class Customer(name: String) {
println("Customer initialized with value ${this.name}") // 放在这里是错误的
init {
println("Customer initialized with value ${this.name}")
}
}
通过构造方法定义类的属性
该构造方法,类自动拥有firstName lastName age属性,不需要额外定义
class Person(val firstName: String, val lastName: String, var age: Int) {
// ...
}
注解和类修饰符
class Customer public @Inject constructor(name: String) { ... }
构造函数不可见:
class Person private constructor () {
}
次级构造函数 通过constructor关键字作为标识
次级构造函数必须要调用主构造函数
class Person constructor(name: String? = "菊菊") {
// 主构造函数内,可定义属性,可定义方法
var name: String? = "张"
var age: Int? = 0
fun isSession(x: Any): Unit {
}
init {
println("Customer initialized with value ${this.name}")
this.name = name
isSession(3)
}
// 次级构造函数
constructor(name: String?, age: Int) : this(name) {
this.age = age
}
constructor(name: String?, age: Int, city: String) : this(name) {
}
}
创建类的实例 不需要new关键字
val invoice = Invoice()
val customer = Customer("Joe Smith")
var person = Person("王五")
val s = person.name ?: return
println(" s = "+ s)
嵌套类和匿名类创建:http://kotlinlang.org/docs/reference/nested-classes.html
Class Members
Classes can contain:
Constructors and initializer blocks
Functions
Properties
Nested and Inner Classes
Object Declarations
Inheritance继承
超类:Any 该类类似java的Object,只有方法没有成员
只有三个方法:equals,hashCode,toString
/**
* The root of the Kotlin class hierarchy. Every Kotlin class has [Any] as a superclass.
*/
public open class Any {
public open operator fun equals(other: Any?): Boolean
public open fun hashCode(): Int
public open fun toString(): String
}
继承通过:操作符
父类必须由open修饰,重写的方法也需要有open修饰
open class Base(p: Int)
class Derived(p: Int) : Base(p)
如果父类没有主构造函数通过super调用
class MyView : View {
constructor(ctx: Context) : super(ctx)
constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
}
// 这是空的构造函数
class MyView() : View(){}
Overriding Methods 重写方法:
open class Base {
open fun v() {}
fun nv() {}
}
class Derived() : Base() {
override fun v() {}
}
Overriding Properties 重写属性
父类的val,子类可以重写成var
open class Foo {
open val x: Int get() { ... }
}
class Bar1 : Foo() {
override val x: Int = ...
}
Calling the superclass implementation
调用父类的方法,属性通过super关键字
open class Foo {
open fun f() { println("Foo.f()") }
open val x: Int get() = 1
}
class Bar : Foo() {
override fun f() {
super.f()
println("Bar.f()")
}
override val x: Int get() = super.x + 1
}
内部类调用外部类:
class Bar : Foo() {
override fun f() { /* ... */ }
override val x: Int get() = 0
inner class Baz {
fun g() {
super@Bar.f() // Calls Foo's implementation of f()
println(super@Bar.x) // Uses Foo's implementation of x's getter
}
}
}
函数重写的规则:继承多个类,重写同一个函数
open class A {
open fun f() { print("A") }
fun a() { print("a") }
}
interface B {
fun f() { print("B") } // interface members are 'open' by default
fun b() { print("b") }
}
class C() : A(), B {
// The compiler requires f() to be overridden:
override fun f() {
super<A>.f() // call to A.f()
super<B>.f() // call to B.f()
}
}
//必须指明 A or B 有两个父类,super.f()不知道调用的是谁的
super<A>.f()
super<B>.f()
Abstract Classes
关键字:abstract
open class Base {
open fun f() {}
}
abstract class Derived : Base() {
override abstract fun f()
}
Kotlin 没有static关键字,所以就没有静态方法和静态变量