基本概念

本质上就是解决如何用程序描述世界的问题

讨论如何把实际存在的东西映射成程序的类和对象

一种程序设计的思路,思想,方法

程序设计层面的概念

设计模式:前人的程序设计经验

学习指南

 面向对象本身就是一个复杂的东西。第一次学习不懂也是难免的、在以后的使用中多多理解就好了

什么是接口

 直观理解就是一种约定

  • Kotin 的接口与Object-C的Protocol比较类似

举例。输入设备接口

interface InputDevice {
fun input(event: Any)
}

不同点 接口

不能有状态

必须由类对其进行实现后使用 

抽象类

实现了一部分协议的半成品

可以有状态,可以有方法实现

必须由子类实现

共同点

  1.  比较抽象,不能直接实例化
  2. 有需要子类(实现类)实现的方法
  3. 父类(接口)变量可以接受子类(实现类)的实例赋值

不同点

 抽象类有状态,接口没有状态

抽象类有方法实现,接口只能有无状态的默认实现

抽象类只能单继承,接口可以多实现

抽象类反映本质,接口提现能力

 

代码示例

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
}
}
//光电鼠标
interface OpticalMouse{}

class Computer {
fun addUSBInputDevice(inputDevice: USBInputDevice) {
// 插入输入设备
println("add usb input device:$inputDevice")
}

fun addBLEInputDevice(inputDevice: BLEInputDevice) {
// 插入输入设备
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("输入设备不支持")
}

}
}
}

fun main(args: Array<String>) {
val computer = Computer()
val mouse = UsBMouse("罗技鼠标")
computer.addInputDevice(mouse)
}

运行结果:add usb input device:罗技鼠标

// 添加抽象后,可以理解为半成品。必须实现后才能用 class 就相当于成品

package com.yzdzy.kotlin.chapter4

import java.lang.IllegalArgumentException

interface InputDevice {
fun input(event: Any)
}

interface USBInputDevice : InputDevice

interface BLEInputDevice : InputDevice
// 添加抽象后,可以理解为半成品。必须实现后才能用 class 就相当于成品

abstract class USBMouse(val name: String) : USBInputDevice, OpticalMouse {
override fun input(event: Any) {

}

override fun toString(): String {
return name
}
}

class LogitechMouse : USBMouse("罗技鼠标") {

}

//光电鼠标
interface OpticalMouse {}

class Computer {
fun addUSBInputDevice(inputDevice: USBInputDevice) {
// 插入输入设备
println("add usb input device:$inputDevice")
}

fun addBLEInputDevice(inputDevice: BLEInputDevice) {
// 插入输入设备
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("输入设备不支持")
}

}
}
}

fun main(args: Array<String>) {
val computer = Computer()
val mouse = LogitechMouse()
computer.addInputDevice(mouse)
}

结果和上面一样

接口和抽象类的区别

package com.yzdzy.kotlin.chapter4

abstract class A {
var i = 0
open fun hello(){

}
abstract fun hell2o()
}

//变量无法被赋值,,就相当于无法定义变量。就相当于接口方法
//接口无法实现
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")
}
}
//单集成,多实现

class DD(override var j: Int) :A(),B,C{
override fun hello() {
TODO("Not yet implemented")
}

override fun hell2o() {
TODO("Not yet implemented")
}

}
// 中心词
abstract class Mouse
//能力
interface Optocal
//下面我们来描述几个案例
//联想笔记本
//<>尖括号是代表接口 {}花括号代表抽象类
//<联想><笔记本>{电脑}
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)
}