I. Introduction
Kotlin
Using the keyword class
declaring class, followed by the class name. Kotlin
The class is the default public final
, so if you do not like is final
when the need to use open
keywords to decorate a class, if not declare the parent class. The default is the parent Any
class.
//定义一个空类
open class Animal
interface Run
//继承类或接口时,使用 : 隔开,父类和接口间使用 , 隔开
class Dog : Animal(), Run
Second, a constructor
1. Main constructor
After the constructor on the main class name can not contain any code, the code can be placed in the initialization code segment initialization, initialization code segment using the init
keyword as a prefix.
注意:
Primary constructor parameter may be used in the initialization code segment may be defined by the properties and the primary constructor initializes attribute value (which may be var
or val
).
class Dog constructor(val age: Int, val name: String){
init {
println("Dog $name is $age years old.")
}
}
If the constructor annotated, or have visibility modifier, constructor
the keyword is necessary, notes and modifiers placed before it, otherwise it constructor
can be omitted.
2. Create an instance of
fun main(args: Array<String>) {
val dog = Dog(2,"Irving")
}
3. Secondary Constructor
Secondary constructor class using constructor
modified.
class Cat {
constructor(age: Int, name: String){
println("Cat $name is $age years old")
}
}
If the main class constructor, then each constructor need secondary agent directly or indirectly, this master builder. Acting another constructor in the same class using this
keywords.
class Cat(age: Int) {
constructor(age: Int, name: String): this(age){
println("Cat $name is $age years old")
}
constructor(age: Int, name: String, type: Int): this(age, name){
println("Cat $name is $age years old")
}
}
Third, access modifier
private:
Modified class, the class represents all members are private. When modifying members, it indicates that the class members are private.protected:
Can only be used to modify the members of the class, he said that only the class and its subclasses can access the member.public:
All classes are accessible.internal:
Indicates only that the modules (moudle
classes in) access.
Fourth, the associated objects
By companion object
statement accompanying the object must be written within a class, as a companion object of this class, the compiler will generate a compile-time static in the class Companion
object and then call the members of this static object. Associated object properties and methods can be understood as Java
the static variables and methods, it can be directly 类名.属性/方法
invoked, if it is Java
through the call class 类名.Companion.属性/方法
calls
class Bird{
companion object {
const val BIRD_NAME = "Irving"
fun getBirdName(): String = BIRD_NAME
}
}
In addition to using object
keyword to declare a singleton class outside, using the companion object can easily create singleton object.
class Person{
companion object {
fun get(): Person = Holder.instance
}
private object Holder{
val instance = Person()
}
}
Five, Kotlin dynamic proxy
Kotlin
By using by
easy to use dynamic proxy keyword.
interface People {
fun sayHello()
}
//目标类
class Man : People {
override fun sayHello() {
println("Hello")
}
}
//代理类
class Irving(people: People) : People by people
fun main(args: Array<String>) {
val irving = Irving(Man())
irving.sayHello()
}
Kotlin
Will be compiled as dynamic static proxy agent to call, so Kotlin
the dynamic proxy than Java
high dynamic proxy efficiency, because Java
the dynamic nature of the agent is through reflection to call.
Sixth, data classes
You can use data
keyword to declare a data type, the data class is public final
, it may not be inherited (seal type may be used if desired inheritance). The compiler will generate data for all the attributes of the class getter()/setter()
methods, and will rewrite toString()
, hashCode()
, equals()
, copy()
of these methods.
data class User(
var id: Int,
var name: String,
var gender: Int
)
Seven, sealed class
Sealed classes can be understood as an enhanced enumeration class, sealed class and its subclasses must be written in the same file, the enhanced sealing place is a subclass of class can be extended to more flexible.
sealed class Hello {
object UP : Hello()
object DOWN : Hello()
object LEFT : Hello()
object RIGHT : Hello()
class Irving(var age: Int) : Hello()
}
fun test(hello: Hello) {
when (hello) {
Hello.UP -> println("UP")
Hello.DOWN -> println("DOWN")
Hello.LEFT -> println("LEFT")
Hello.RIGHT -> println("RIGHT")
is Hello.Irving -> println(hello.age)
}
}
fun main(args: Array<String>) {
test(Hello.Irving(11))
}