A constructor
Class constructors in Kotlin can be written similarly to those in Java:
class Rect{
var h:Float;
var w:Float;
constructor(h: Float,w: Float) {
this.h = h
this.w = w
}
}
Copy the code
This defines a rectangle class with two properties (length H, width W). Constructor declares the keyword for its constructor. Note here that defining a property in Kotlin requires either an initial value for the property or a constructor that holds the property assignment. Otherwise the program will make an error.
You can also write:
class Rect(var h: Float,var w: Float){
}
Copy the code
This writing can define both the properties of the class and declare constructors. For a more detailed description of constructors, see Kotlin’s constructor
inheritance
Inheritance in Kotlin is declared as class subclass name: superclass name {}
class Square : Rect(){
}
Copy the code
There is a parent class Rect and a subclass Square. Note that the parent class needs to declare the open keyword before it can be inherited by subclasses.
open class Rect{
}
Copy the code
On method overrides, subclasses need to declare the override keyword to declare the method to override the parent class. The corresponding parent class also needs to declare the open keyword in the corresponding method
open class Rect{
open fun S(){
}
}
class Square : Rect(){
override fun S(){
}
}
Copy the code
An abstract class
Kotlin’s abstract class definition is similar to That of Java, so we’ll go straight to the code:
abstract class Animal(var name: String){
abstract fun type();
}
class Dog(name: String):Animal(name){
override fun type() {
}
}
Copy the code
Use the abstract keyword to declare the Animal class as an abstract class with an abstract method of Type. The Dog class automatically overrides this type method when it inherits it and requires the override keyword.
Interface definition and inheritance
Kotlin’s interface definition and inheritance are roughly the same as Java writing. Here is an example of a subclass inheriting both a parent class and an interface:
abstract class Animal(var name: String){
abstract fun type();
}
interface Ability{
fun shout();
}
class Dog(name: String):Animal(name),Ability{
override fun shout() {
}
override fun type() {
}
}
Copy the code
Is the keyword
In Java polymorphism we can use an object of a parent class to determine whether it is a reference to a subclass by the instanceof keyword. Similarly, Kotlin can use the is keyword.
fun main(args: Array<String>) { var a: Animal = Dog("Boby"); if(a is Dog){ println(a.name); }}Copy the code
Output result:
Boby
Copy the code
Agency and commission
Kotlin can implement the proxy pattern using the by keyword, as you can see: Kotlin delegate
The singleton
Kotlin can declare a class as a singleton using the object keyword.
object SignalInstance{
}
Copy the code
We can try to define both variables as SignalInstance.
var b = SignalInstance;
var c = SignalInstance;
println(b == c);
Copy the code
Output result:
true
Copy the code
Enumeration type
Kotlin defines an enum type through the enum class keyword. Such as:
enum class NUMBER{
ONE,TWO,THREE,FOUR,FIVE
}
Copy the code
The class name. Property format is used to obtain the property:
println(NUMBER.ONE);
Copy the code
Output result:
ONE
Copy the code
The position of this attribute in the enumeration can be obtained using the ordinal keyword:
println(NUMBER.ONE.ordinal);
Copy the code
Output result:
0
Copy the code
Seal type
The sealed keyword defines a seal class, which allows a class to have a finite number of subclasses. The seal class can even be understood as a special enumeration class. The seal class itself cannot be instantiated.
sealed class Option(var num1:Int,var num2:Int){ class add(num1: Int,num2: Int):Option(num1,num2){ fun add() = num1+num2; } class sub(num1: Int,num2: Int):Option(num1,num2){ fun sub() = num1-num2; }}Copy the code
It is an error to define the following variables in the main function:
Var x: Option = Option(1,1);Copy the code
This is because the Option class is a seal class and cannot be instantiated itself.
Here is a code that uses the seal class:
Fun main(args: Array<String>) {var a: option.add (2,3); Var b: Option = option.sub (3,2); var list = listOf<Option>(a,b); for (o in list){ if(o is Option.add){ println(o.add()); }else if(o is Option.sub){ println(o.sub()); }}}Copy the code
The object defined above by two subclasses of the stamp class of Option is output by calling the non-method in its subclass. The seal class can define a limited number of subclasses, each with its own custom content, which is the biggest difference from enumeration.