//: Playground - noun: a place where people can play import UIKit ///-----继承-------// //1.继承是Swift中类区别于结构和枚举的唯一特征 //2.基类:任何一个不继承于其它类的类被称为基类。 //1.继承的基本使用 class Person { var name:String = "" var age:Int = 0 func eat() -> String { return "(name)在吃饭" } } class Student: Person { //定义子类中独有的属性 var id : Int = 0 var score : Double = 0.0 func study() -> String { self.eat() super.eat() //调用父类方法 return "(name) 在学习" } } var p1 = Person() p1.name = "小明" p1.eat() var s1 = Student() s1.name = "小宝" s1.study() //2. 方法的重写:(1)必须有继承关系 (2)使用override关键字来重写父类方法 class Car { var name : String = "" var color : String = "" func start() { print("Car类中的启动方法") } } class Taxi : Car { var company : String = "" func saleTickets() { print("Taxi在售票") } //子类重写父类的方法 override func start() { super.start() print("Taxi类重写的start方法") } } var car = Car() car.name = "宝马" car.color = "Black" car.start() var taxi = Taxi() taxi.name = "捷达" taxi.color = "Blue" taxi.company = "不知名公司" taxi.saleTickets() taxi.start()