本例是基于安卓开发环境的。我用的Android Studio 写的
现在,有这样3个身份:老大(Activity)、助理(Assistant)、干活的人(Person)
只要是人,就要吃饭,所有,有个抽象基类(Human)定义吃饭,老大手底下的人,需要帮他收钱和干活
/**
* 抽象基类
*/
abstract class Human {
abstract fun eat()
}
/**
* 收钱
*/
interface IMoney {
fun shouqian()
}
/**
* 办事
*/
interface IWork {
fun working()
}
现在,老大给助理和底层人员都分派了任务:收钱和干活。
/**
* 助理
*/
class Assistant:Human(),IMoney,IWork {
override fun eat() {
println("吃的好")
}
override fun shouqian() {
println("助理,收钱")
}
override fun working() {
println("助理,工作")
}
}
/**
* 干活的人
*/
class Person : Human(), IMoney, IWork {
override fun eat() {
println("吃的一般")
}
override fun shouqian() {
println("收钱")
}
override fun working() {
println("工作")
}
}
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)