Kotlin foundation - classes and interfaces

Keywords: Java Attribute

(1) Definition of class

Definition of classes in Java:

public class SimpleClass{

}

Definition of classes in Kotlin:

class SimpleClass{}

Note: in Kotlin, if there is no modifier, it is public by default. If there is no content in the class, you can omit the braces, and write as follows:

class SimpleClass

(2) Variables and methods in class

Variables and methods in Java classes

public class SimpleClass{
      public int x;
      public void y(){

      }

}

Variables and methods in the Kotlin class:

class SimpleClass{
      var x:Int=0 //Must be initialized
      fun y(){

      }
}

Note: variable definitions in Kotlin must be initialized

(3) Constructor in class

Constructor of class in Java:

public class SimpleClass{
      public int x;
      public SimpleClass(int x){
          this.x=x;
      }

}

Constructors for classes in Kotlin:

class SimpleClass{
    var x:Int 
     constructor(x:Int){
         this.x=x;
     }
}

Or write:

class SimpleClass constructor(x:Int){
        var x:Int=x
}

Or omit the constructor, which can be written as follows:

class SimpleClass(x:Int){
        var x:Int=x
}

Or write:

class SimpleClass(var x:Int){
        
}

Note: in Kotlin, there are two kinds of constructors: the primary constructor and the second constructor. The primary constructor requires all other constructors to call it. That is to say, there is only one instance path for a class.

(4) Class instantiation

Instantiation of classes in Java:

SimpleClass simpleClass=new SimpleClass(9);
System.out.println(simpleClass.x);
simpleClass.y();

Instantiation of classes in Kotlin:

val simpleClass=SimpleClass(9)
println(simpleClass.x)
simpleClass.y()

(5) Definition of interface

Interface in Java:

public interface SimpleInf{
       void simpleMethod();
}

Interfaces in Kotlin:

interface SimpleInf{
       fun simpleMethod(){

       }
}

Note: the interface in Kotlin is also the default public

(6) Implementation of interface

Implementation of interface in Java:

public class SimpleClass implements SimpleInf{

        @Override
        public void simpleMethod() {

        }
}

Implementation of the interface in Kotlin:

class SimpleClass:SimpleInf{
        override fun simpleMethod() {

        }
}

(7) Definition of abstract class

Definition of abstract classes in Java:

 public abstract class AbsClass{
        public abstract void absMethod();
        protected void overridable(){}
        public final void nonOverridable(){}
}

Definition of abstract class in Kotlin:

abstract class AbsClass{
        abstract fun absMethod()
        open fun overridable(){}  //Note: to rewrite non abstract methods in Kotlin's abstract classes by subclasses, open keyword must be added
        fun nonOverridable(){} //Note: non abstract methods in Kotlin's abstract classes cannot be overridden by subclasses by default
}

(8) Class inheritance

Inheritance of classes in Java:

public class SimpleClass extends AbsClass implements SimpleInf{

        @Override
        public void simpleMethod() {

        }

        @Override
        public void absMethod() {

        }
}

Inheritance of classes in Kotlin:

class SimpleClass:SimpleInf, AbsClass() {
        override fun simpleMethod() {

        }

        override fun absMethod() {

        }

}

Note: if there are two categories, A and B. Class A inherits class B, so class B is either abstract or modified by open. As follows:

open class SimpleClass:SimpleInf, AbsClass() {
        override fun simpleMethod() {

        }

        override fun absMethod() {

        }

}
    
class SimpleClass2 : SimpleClass(){
        
}

(9) JavaBean writing

JavaBean writing in Java:

 

public class Person {
    private int age;
    private String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

JavaBean writing in Kotlin:

class Person(age:Int,name:String) {
    var age:Int=age
    var name:String=name
}

If you want to customize the get and set methods, you can also write as follows:

class Person(age: Int, name: String) {
    var age: Int = age
        get() {
            return field
        }
        set(value) {
            field = value
        }
    var name: String = name
        get() {
            return field
        }
        set(value) {
            field = value
        }
}

(10) Property reference

Attribute reference in Kotlin:

val ageRef=Person::age //Unbound Receiver
val person=Person(18,"zhangsan")
val nameRef=person::name //Bind Receiver

ageRef.set(person,20)
nameRef.set("lisi")

 

Published 41 original articles, won praise 6, visited 30000+
Private letter follow

Posted by chrisv on Thu, 05 Mar 2020 03:11:43 -0800