Classes and interfaces in kotlin

Time:2022-3-29

1、 Foreword

< font face = bold > inBuilt in type of kotlin learning notes (1)andBuilt in type of kotlin learning notes (2)In, we have talked about the functions in kotlin built-in typesBasic typearraysectionCollection frameworkandfunction

2、 Classes and interfaces in kotlin

2.1 definition of class

Java:

//Simple class
public class SimpleClass {
}

//Classes with properties and methods
public class SimpleClass {
    public int x;
    public void y() {
    }
}

//Class with construction method
public class SimpleClass {
    public int x;
    public SimpleClass(int x) {
        this.x = x;
    }
}

Kotlin:

  1. < font face = bold > the default is public;
  2. < font face = bold > there is no content inside the class, and the brackets can be omitted‘
  3. The attributes in the < font face = bold > class must be initialized or delayed.
//Simple class
Class simpleclass // the default is public

//Classes with properties and methods
class SimpleClass {
    Var X: int = 0 // must be initialized
    fun y() {
    }
}

//Class with construction method
class SimpleClass {
    Var X: int = 0 // must be initialized
    Constructor (X: int) {// sub constructor
        var x: Int = x
    }
}
< = = > // equivalent writing
Class simpleclass constructor (X: int) {// main constructor
    var x: Int = x
}
< = = > // equivalent writing
Class simpleclass (X: int) {// main constructor
    var x: Int = x
}
< = = > // equivalent writing
Class simpleclass (VaR X: int) {// main constructor

}

2.2 class instantiation

Java:

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

Kotlin:

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

2.3 definition of interface

Java:

public interface SimpleInf {
    void simpleMethod();
}

Kotlin:

interface SimpleInf {
    fun simpleMethod()
}

2.4 implementation of interface

Java:

public class SimpleClass implements SimpleInf {
    @Override
    public void simpleMethod() {
    }
}

Kotlin:

class SimpleClass(var x: Int): SimpleInf {
    override fun simpleMethod() {
    }
}

2.5 definition of abstract class

Java:

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

Kotlin:

abstract class AbsClass {
    abstract fun absMethod()
    
    //Not an abstract method or an interface method  
    //A method that must add the open keyword can be overridden
    open fun overridable(){}                             
    fun nonOverridable(){}
}

2.6 class inheritance

Java:

public class SimpleClass extends AbsClass implements SimpleInf {

}

Kotlin:

class SimpleClass(var x: Int): AbsClass(), SimpleInf {

}

2.7. Property

< font face = bold > property in kotlin is actually equal to field + getter + setter in Java.

Java:

public class Person {
    private int age; //field
    
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        System.out.println("setAge: " + age);
        this.age = age;
    }
}

Kotlin:

class Person(age: Int, name: String) {
    var age: Int = age //property
        get() {
            return field
        }
        set(value) {
            println("setAge: $value")
            field = value
        }
}

2.8. Attribute reference

fun main() {
    Val ageref = person:: age // unbound receiver
    val person = Person(18, "Test1")
    Val nameref = person:: name // bind receiver
    ageRef. Set (person, 20) // need to pass to receiver
    nameRef. Set ("test2") // no need to pass to receiver
}

2.9. Review of classes and interfaces in kotlin

Classes and interfaces in kotlin

3、 Extension method in kotlin

< font face = bold > extension method means that there is no method defined in the class. We can extend it, that is, add a method. Java does not have the concept of extension methods.

3.1 definition of extension method

< font face = bold > for example, I want to define an extension method to check whether the mailbox is legal for the string class, which is written as follows:

fun String.isEmail(): Boolean {
    Return matches (regex)
}

< font face = bold > Call:

"[email protected]".isEmail()

4、 Summary

< font face = bold > this blog mainly talks about the in kotlinClasses and interfacesandExtension method, let’s talk about it in the next sectionEmpty type securityandSmart type conversion

5、 Source code

Source codeIt has been uploaded to GitHub and can be downloaded directly if necessary.

Recommended Today

Windows terminal uses nushell+oh-my-posh to beautify

First look at the renderings Please point out if the steps are wrong. Install nushell from github Download from github releaseRelease 0.66.2 · nushell/nushell (github.com) github release download speed may be slow, you can download it hereDownload (serctl.com) Install windows terminal in microsoft store Install oh-my-posh on Microsoft Store Winget can also be used, but […]