Kotlin programming interface and implementation interface

Source: Internet
Author: User
Tags abstract class definition modifier

Kotlin Programming related knowledge points : Kotlin programming using IntelliJ IED and understanding Source files (. kt) Kotlin programming and other properties Kotlin programming methods such as using Kotlin programming Kotlin the parent class of programming and inheriting the parent class

This article summarizes:

Interface Introduction

implementing Interfaces Define interfaces :

Introduced:

The Kotlin interface is much like Java 8. They can all contain abstract methods, as well as the implementation of methods. Unlike an abstract class, an interface cannot hold state. can have attributes but must be abstract, or provide an implementation of accessors.

Define an interface that contains an abstract property, an accessor implementation of a property, an abstract method, an implementation of a method, and write code as follows:

Package Com.xingen.kotlin Fun

Main (args:array<string>) {
    var instance = Test2015526 ()
    println ( Instance.property)
    println (INSTANCE.PROPERTYIML)

    instance.test ()
    instance.test1 ()
}

Class Test2015526:testinterface {
    override Val property:string get () = "Implement class defines the implementation of the properties in the interface"

    override fun Test1 () {
        PR INTLN ("overriding interface method in implementation class")
    }
}

/**
 * Customizing an interface */
interface TestInterface {
    /**
     * Abstract Properties *
    /val property:string

    /**
     *  accessor (here refers to the property's get ()) implementation, that is, the implementation of the abstract property
     */
    Val Propertyiml:string get () = "Implementation of the internal definition attribute of the interface"

    /**
     * Method Implementation */Fun
    Test () {
        println (" Output custom Interface Method ")
    }

    /**
     * Abstract method *
     /Fun
    test1 ()
}

Output Result:

Implementing an implementation of a property within a class definition interface an implementation of an interface
-defined property
outputs a custom interface method 

know:

Defining interfaces with keyword interface

A class can implement one or more interfaces

There are two methods in the interface (the implementation of abstract methods and methods), with two properties (abstract properties and implementation of property accessors), which are very different from Java.

Only the Val-modified read-only attribute in the interface, no writable property with Var modifier

Implement interface, add after class header: interface name

To implement an interface, you must override the abstract property and the abstract method, and use the override modifier.

Methods and properties of an interface the default open-decorated interface and the parent class have a method with the same name

Package Com.xingen.kotlin Fun

Main (str:array<string>) {
    test<string> ("This class is"). Test ()
}
Class Test<t> constructor (override Var t:t): Basetest<t> (T), testinterface {
    /**
     * Copy method in parent class
     */
    override Fun Test () {

        println ("${t} Test Subclass")
        //Call Interface Test ()
        super<testinterface>.test ()
        //Call the parent class of Test ()
        super<basetest>.test ()}
}
/**
 * Custom Parent class */
Open Class basetest<t> (open Val t:t) {
    Open Fun Test () {
        println ("${t} Basetest Parent class")
    } fun
   closetes T () {
        println ("a method that cannot be overridden in a parent class)
   }
}}

/**
 * Customizing an interface *
 /
interface  testinterface{
    //interface member variable default is open fun  test () {  println ("Output Custom Interface Method")}
}

Output Result:

This class is the test subclass 
Output Custom interface method 
This class is the Basetest parent class 

From the above code:

Implement an interface that is appended to the class header: the interface name, and the inherited parent class is: Parent class name ()

The default open decoration of the method of the interface

When the parent class and interface or multiple interfaces have the same name method, the call should use the super< class name, called by the method with the same name.

Call the interface's test ()
super<testinterface>.test ()
//Call the parent class's Test ()
super<basetest>.test ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.