-
In essence, it is to solve the problem of how to describe the world with programs
Discuss how to map the real things into the classes and objects of the program
A thought, thought and method of programming
Concept of programming level
Design pattern: previous programming experience
Learning GuideObject oriented itself is a complex thing. It's inevitable that you don't understand for the first time. It's better to understand more in later use
What is an interfaceIntuitive understanding is a kind of convention
- The interface of Kotin is similar to the Protocol of Object-C
give an example. Input device interface
interface InputDevice { fun input(event: Any) }
Different point interfaceNo status
It must be implemented by a class before it can be used
abstract classPart of the protocol's semi-finished products are realized
Can have state, can have method to realize
Must be implemented by subclass
common ground- It is abstract and cannot be instantiated directly
- Methods that need to be implemented by subclasses (implementation classes)
- The parent class (Interface) variable can accept the instance assignment of the child class (implementation class)
Abstract class has state, interface has no state
Abstract class has method implementation, interface can only have default implementation of stateless
Abstract classes can only be inherited by one, and interfaces can be implemented by many
Abstract class reflects the nature, interface presentation capability
Code example
package com.yzdzy.kotlin.chapter4 import java.lang.IllegalArgumentException interface InputDevice { fun input(event: Any) } interface USBInputDevice : InputDevice interface BLEInputDevice : InputDevice class UsBMouse(val name:String) : USBInputDevice,OpticalMouse { override fun input(event: Any) { } override fun toString(): String { return name } } //Photoelectric mouse interface OpticalMouse{} class Computer { fun addUSBInputDevice(inputDevice: USBInputDevice) { // Insert input device println("add usb input device:$inputDevice") } fun addBLEInputDevice(inputDevice: BLEInputDevice) { // Insert input device println("add ble input device:$inputDevice") } fun addInputDevice(inputDevice: InputDevice) { when (inputDevice) { is BLEInputDevice -> { addBLEInputDevice(inputDevice = inputDevice) } is USBInputDevice -> { addUSBInputDevice(inputDevice = inputDevice) } else -> { throw IllegalArgumentException("Input device not supported") } } } } fun main(args: Array<String>) { val computer = Computer() val mouse = UsBMouse("Logitech mouse") computer.addInputDevice(mouse) }
Run result: add usb input device: Logitech mouse
//After adding abstraction, it can be understood as semi-finished product. It must be implemented before class can be used, which is equivalent to finished productpackage com.yzdzy.kotlin.chapter4 import java.lang.IllegalArgumentException interface InputDevice { fun input(event: Any) } interface USBInputDevice : InputDevice interface BLEInputDevice : InputDevice // After adding abstraction, it can be understood as semi-finished product. It must be implemented before class can be used, which is equivalent to finished product abstract class USBMouse(val name: String) : USBInputDevice, OpticalMouse { override fun input(event: Any) { } override fun toString(): String { return name } } class LogitechMouse : USBMouse("Logitech mouse") { } //Photoelectric mouse interface OpticalMouse {} class Computer { fun addUSBInputDevice(inputDevice: USBInputDevice) { // Insert input device println("add usb input device:$inputDevice") } fun addBLEInputDevice(inputDevice: BLEInputDevice) { // Insert input device println("add ble input device:$inputDevice") } fun addInputDevice(inputDevice: InputDevice) { when (inputDevice) { is BLEInputDevice -> { addBLEInputDevice(inputDevice = inputDevice) } is USBInputDevice -> { addUSBInputDevice(inputDevice = inputDevice) } else -> { throw IllegalArgumentException("Input device not supported") } } } } fun main(args: Array<String>) { val computer = Computer() val mouse = LogitechMouse() computer.addInputDevice(mouse) }
The result is the same as above
The difference between interface and abstract class
package com.yzdzy.kotlin.chapter4 abstract class A { var i = 0 open fun hello(){ } abstract fun hell2o() } //Variable cannot be assigned, which means variable cannot be defined. It's like an interface method //Interface cannot be implemented interface B { var j: Int fun hello() { print(j) } } interface C abstract class E class D(override var j: Int) : B { } class G : A() { override fun hello() { super.hello() } override fun hell2o() { TODO("Not yet implemented") } } //Single integration, multi implementation class DD(override var j: Int) :A(),B,C{ override fun hello() { TODO("Not yet implemented") } override fun hell2o() { TODO("Not yet implemented") } } // Central word abstract class Mouse //ability interface Optocal //Let's describe a few cases //Lenovo notebook //The angle bracket is for interface fun main(args: Array<String>) { val d = DD(0) if(d is A){ } if(d is B){ } val a: A = DD(j = 0) val b: B = DD(0) val c: C = DD(0) }
17 June 2020, 01:07 | Views: 1396
Add new comment
0 comments