编辑代码

//协议的声明
protocol Person {
    var name: String { get set}
    var age: Int { get }
}
//类遵守协议
class Student: Person {
    var name : String
    var age : Int
    init(){
        name = ""
        age = 0
    }
}
//协议中的关联类型
protocol Person {
    associatedtype UnknownType
    var name: String { get set}
    var age: Int { get }
    var weight: UnknownType { get }
}

class Student: Person {
    var name : String
    var age : Int
    var weight: Double
    init(){
        name = ""
        age = 0
        weight = 0.0
    }
}
//协议的继承性
protocol Person {
    associatedtype UnknownType
    var name : String {get set}
    var age : Int {get set}
    var weight: UnknownType { get }
    func personDescription()
}
protocol Student {
    var school : String {get set}
    func studentDescription()
}
protocol Graduate : Person, Student {
    var supervisor : String {get set}
    func graduateDescription()
}
class ComputerAssociationMember : Graduate {
    var name : String = ""
    var age : Int
    var weight: Double
    var school : String
    var supervisor : String
    func personDescription() {
        print("It's person description")
    }
    func studentDescription() {
        print("It's student description")
    }
    func graduateDescription() {
        print("It's graduate description")
    }
    init(name : String, age : Int, weight: Double, school : String, supervisor :String){
        self.name = name
        self.age = age
        self.weight = weight
        self.school = school
        self.supervisor = supervisor
    }
}
let theMember = ComputerAssociationMember(name: "Tom", age: 23, weight: 69.2, school: "BUAA", supervisor: "Ian")
theMember.personDescription()
theMember.studentDescription()
theMember.graduateDescription()