跳到主要内容

Swift中方法和属性的覆盖

提示
  1. 方法重写的概念:在Swift中,子类可以使用override关键字重写超类的方法,实现自定义行为。
  2. 访问和防止重写超类方法:子类通过super关键字访问超类方法,而超类可用final关键字阻止其方法被重写。
  3. 重写计算属性:Swift允许重写计算属性,但不能重写存储属性。

Swift 继承 中,子类继承了超类的方法和属性。这使得子类可以直接访问超类的成员。

现在,如果超类和子类中都定义了相同的方法,那么子类的方法会重写超类的方法。这被称为重写。

我们使用 override 关键字来声明方法重写。例如,

class Vehicle {

func displayInfo(){
...
}
}

class Car: Vehicle {

// 重写方法
override func displayInfo() {
...
}
}

这里,Car 子类的 displayInfo() 方法重写了 Vehicle 超类的同名方法。

示例:Swift 方法重写

class Vehicle {

// 超类中的方法
func displayInfo() {
print("四轮车或两轮车")
}
}

// Car 继承 Vehicle
class Car: Vehicle {

// 重写 displayInfo() 方法
override func displayInfo() {
print("四轮车")
}
}

// 创建子类的对象
var car1 = Car()

// 调用 displayInfo() 方法
car1.displayInfo()

输出

四轮车

在上面的例子中,我们重写了超类 Vehicle 中的 displayInfo() 方法,放在子类 Car 里面。

// 在 Car 类内部
override func displayInfo() {
print("四轮车")
}

这里,我们使用了 override 关键字来指定被重写的方法。

现在,当我们使用 Car 类的对象 car1 调用 displayInfo() 方法时,

car1.displayInfo()

会调用子类中的方法。

Swift 方法重写

这是因为 Car 子类的 displayInfo() 方法重写了 Vehicle 超类的同名方法。

在 Swift 中访问被重写的方法

要从子类访问超类的方法,我们使用 super 关键字。例如,

class Vehicle {

// 超类中的方法
func displayInfo() {
print("Vehicle: 四轮车或两轮车")
}
}

// Car 继承 Vehicle
class Car: Vehicle {

// 重写 displayInfo() 方法
override func displayInfo() {

// 访问超类的 displayInfo()
super.displayInfo()
print("Car: 四轮车")
}
}

// 创建子类的对象
var car1 = Car()

// 调用 displayInfo() 方法
car1.displayInfo()

输出

Vehicle: 四轮车或两轮车
Car: 四轮车

在上面的例子中,Car 子类的 displayInfo() 方法重写了 Vehicle 超类的同名方法。 在 CardisplayInfo() 中,我们使用了

// 调用超类的方法
super.displayInfo()

来调用 VehicledisplayInfo() 方法。

所以,当我们使用 car1 对象调用 displayInfo() 方法时

// 调用 displayInfo() 方法
car1.displayInfo()

被重写的方法和超类的 displayInfo() 方法都会被执行。

防止方法被重写

在 Swift 中,我们可以阻止方法被重写。

要使方法不可被重写,我们在超类中声明方法时使用 final 关键字。例如,

class Vehicle {

// 防止重写
final func displayInfo() {
print("是四轮还是两轮车")
}
}

// Car 继承 Vehicle
class Car: Vehicle {

// 尝试重写
override func displayInfo() {
print("四轮车")
}
}

// 创建子类的对象
var car1 = Car()

// 调用 displayInfo() 方法
car1.displayInfo()

在上述示例中,我们在超类中将 displayInfo() 方法标记为 final

一旦方法被声明为 final,我们就无法重写它。所以当我们尝试重写最终方法时,

override func displayInfo() {
print("四轮车")

我们会收到错误消息:“error: instance method overrides a 'final' instance method”。

重写 Swift 属性

在 Swift 中,我们可以重写计算属性。例如,

class University {

// 计算属性
var cost: Int {
return 5000
}
}

class Fee: University {
// 重写计算属性
override var cost: Int {
return 10000
}
}

var amount = Fee()

// 访问费用属性
print("新费用:", amount.cost)

输出

新费用: 10000

在上述示例中,我们在超类 University 内创建了一个计算属性。注意子类 Fee 中的代码,

override var cost: Int {
return 10000
}

我们正在重写计算属性 cost。现在,当我们使用 Fee 的对象 amount 访问 cost 属性时,

// 访问费用属性
amount.cost

调用的是子类内的属性。

注意:我们不能重写 Swift 中的存储属性。例如,

class A {
// 存储属性
var num = 0
}

class B: A {
// 重写存储属性
override var num = 2 // 错误代码
}