Kotlin Creating an anonymous object instance of an interface or abstract class
- 2021-11-30 01:17:50
- OfStack
1. Define interfaces and abstract classes
interface IPerson{
// Get the name
fun getName():String
// Obtain ID card ID
fun getID():String
}
abstract class BaseAnimal{
abstract fun getVoice():String
}
2. Create the corresponding anonymous object
object : IPerson {
override fun getName(): String = "jason"
override fun getID(): String = "00000123"
}
object : BaseAnimal() {
override fun getVoice() = " Wangwang calls "
}
Additional knowledge: android Kotlin inheritance, derivation, interfaces, constructions, methods, property overrides
Preface
kotlin, as the official android development language of google, is the general trend. It is reported that kotlin will completely replace java in android at the end of 2018. In fact, there is no worry about this. After all, kotin and java can interoperate 100%. It is also possible to write in two languages.
Kotlin inheritance
1. Decorate with the open keyword
2. The main constructor follows the function declared immediately after the class
open class Person(var name : String, var age : Int){// Base class
}
class Student(name : String, age : Int, var no : String, var score : Int) : Person(name, age) {
}
// 2 Level constructor
calss Student : Person {
constructor(ctx: Context) : super(ctx) {
}
constructor(ctx: Context, attrs: AttributeSet) : super(ctx,attrs) {
}
}
Another way to write, base class constructor, secondary constructor
/** User base class **/
open class Person(name:String){
/** Secondary constructor **/
constructor(name:String,age:Int):this(name){
// Initialization
println("------- Base class secondary constructor ---------")
}
}
/** Subclass inheritance Person Class **/
class Student:Person{
/** Secondary constructor **/
constructor(name:String,age:Int,no:String,score:Int):super(name,age){
println("------- Inheritance class secondary constructor ---------")
println(" Student name: ${name}")
println(" Age: ${age}")
println(" Student number: ${no}")
println(" Achievements: ${score}")
}
}
fun main(args: Array<String>) {
var s = Student("Runoob", 18, "S12345", 89)
}
Method override
The base class fun function defaults to the final modifier and cannot be overridden on a subclass
Need to add open decorative symbol
Method, the method with the same name gets
1 class from other classes or interfaces (inherited from the implementation of the method), the method with the same name, in the subclass must be displayed for calling
open class A {
open fun f () { print("A") }
fun a() { print("a") }
}
interface B {
fun f() { print("B") } // The member variables of the interface default to open Adj.
fun b() { print("b") }
}
class C() : A() , B{
override fun f() {
super<A>.f()// Call A.f()
super<B>.f()// Call B.f()
}
}
fun main(args: Array<String>) {
val c = C()
c.f();
}
open class A {
open fun f () { print("A") }
fun a() { print("a") }
}
interface B {
fun f() { print("B") } // The member variables of the interface default to open Adj.
fun b() { print("b") }
}
class C() : A() , B{
override fun f() {
super<A>.f()// Call A.f()
super<B>.f()// Call B.f()
}
}
fun main(args: Array<String>) {
val c = C()
c.f();
}
Property override
Attribute overrides use the override keyword. Attributes must have compatible types. Each declared attribute can be overridden by an initializer or an getter method:
open class Foo {
open val x: Int get { ... }
}
class Bar1 : Foo() {
override val x: Int = ...
}
You can override an val attribute with an var attribute, but not the other way around. Because the val attribute itself defines the getter method, overriding the var attribute will declare an additional setter method in the derived class
You can use the override keyword in the main constructor as part 1 of the attribute declaration:
interface Foo {
val count: Int
}
class Bar1(override val count: Int) : Foo
class Bar2 : Foo {
override var count: Int = 0
}
Kotlin interface
The Kotlin interface is similar to Java 8 in that it uses the interface keyword to define the interface, allowing methods to have default implementations:
interface MyInterface {
fun bar() // Not implemented
fun foo() { // Implemented
// Optional method body
println("foo")
}
}
Attributes in the interface
Attributes in an interface can only be abstract, initialization values are not allowed, and the interface does not save attribute values. When implementing an interface, attributes must be overridden:
interface MyInterface{
var name:String //name Attribute , Abstract
}
class MyImpl:MyInterface{
override var name: String = "runoob" // Overloaded attribute
}
Function override
When implementing multiple interfaces, you may encounter the problem of inheriting multiple implementations from the same 1 method. For example:
Instances
interface A {
fun foo() { print("A") } // Implemented
fun bar() // Not implemented, no method body, abstract
}
interface B {
fun foo() { print("B") } // Implemented
fun bar() { print("bar") } // Implemented
}
class C : A {
override fun bar() { print("bar") } // Rewrite
}
class D : A, B {
override fun foo() {
super<A>.foo()
super<B>.foo()
}
override fun bar() {
super<B>.bar()
}
}
fun main(args: Array<String>) {
val d = D()
d.foo();
d.bar();
}
The output is:
ABbar
In the example, interfaces A and B both define methods foo () and bar (), both of which implement foo (), and B implements bar (). Because C is a concrete class that implements A, you must override bar () and implement this abstract method.
However, if we derive D from A and B, we need to implement all the methods inherited from multiple interfaces and indicate how D should implement them.
This 1 rule applies to both methods that inherit a single implementation (bar ()) and methods that inherit multiple implementations (foo ()).