参考:
根据下面的结构 设计类和协议

//交通工具类
class Vehicle: NSObject {
func run(v : Vehicle) {
print("\(v) is runing")
}
}
//动物类
class Animal: NSObject {
func eat(v : Animal) {
print("\(v) is eating")
}
}
//能飞的协议
protocol Flayable {
}
extension Flayable {
func flay(f : Flayable) {
print("\(f) is can flay")
}
}
//能飞的协议
protocol PetType {
}
extension PetType {
func pet(f : PetType) {
print("\(f) is a pet")
}
}
//鹦鹉 宠物 / 能飞 class默认遵守 CustomStringConvertible协议喔
class Parrot : Animal , PetType , Flayable
{
override var description:String {
return "Parrot"
}
}
使用:
let yw = Parrot()
yw.flay(f: yw)
yw.pet(f: yw)

网友评论