Swift 覆盖方法和属性

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

现在,如果在父类和子类中都定义了相同的方法,那么子类中的方法就会覆盖父类中的方法。这被称为覆盖。

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

class Vehicle {
  
  func displayInfo(){
    ... 
  }
}

class Car: Vehicle {
  
  // override method
  override func displayInfo() {
    ... 
  }   
} 

在这里,Car 子类中的 displayInfo() 方法覆盖了 Vehicle 父类中同名的方法。


示例:Swift 方法覆盖

class Vehicle {

  // method in the superclass
  func displayInfo() {
    print("Four Wheeler or Two Wheeler")
  }
}

// Car inherits Vehicle
class Car: Vehicle {

  // overriding the displayInfo() method
  override func displayInfo() {
    print("Four Wheeler")
  }
}

// create an object of the subclass
var car1 =  Car()

// call the displayInfo() method
car1.displayInfo()

输出

Four Wheeler

在上面的示例中,我们在 Car 子类中覆盖了父类 VehicledisplayInfo() 方法。

// inside the Car class 
override func displayInfo() {
  print("Four Wheeler")
}

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

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

car1.displayInfo()

调用的是子类中的方法。

Swift Method Override
方法覆盖的工作原理

这是因为 Car 子类中的 displayInfo() 方法覆盖了 Vehicle 父类中同名的方法。


在 Swift 中访问被覆盖的方法

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

class Vehicle {

  // method in the superclass
  func displayInfo() {
    print("Vehicle: Four Wheeler or Two Wheeler")
  }
}

// Car inherits Vehicle
class Car: Vehicle {

  // overriding the displayInfo() method
  override func displayInfo() {

    // access displayInfo() of superclass
    super.displayInfo()
    print("Car: Four Wheeler")
  }
}

// create an object of the subclass
var car1 =  Car()

// call the displayInfo() method
car1.displayInfo()

输出

Vehicle: Four Wheeler or Two Wheeler
Car: Four Wheeler

在上面的示例中,Car 子类中的 displayInfo() 方法覆盖了 Vehicle 父类中同名的方法。

CardisplayInfo() 方法内部,我们使用了

// call method of superclass
super.displayInfo()

来调用 VehicledisplayInfo() 方法。

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

// call the displayInfo() method
car1.displayInfo()

被覆盖的方法和父类版本的 displayInfo() 方法都会被执行。


防止方法覆盖

在 Swift 中,我们可以阻止方法被覆盖。

为了使一个方法不可被覆盖,我们在父类中声明该方法时使用 final 关键字。例如,

class Vehicle {

  // prevent overriding
  final func displayInfo() {
    print("Four Wheeler or Two Wheeler")
  }
}

// Car inherits Vehicle
class Car: Vehicle {

  // attempt to override
  override func displayInfo() {
    print("Four Wheeler")
  }
}

// create an object of the subclass
var car1 =  Car()

// call the displayInfo() method
car1.displayInfo()

在上面的示例中,我们将父类中的 displayInfo() 方法标记为 final

一旦方法被声明为 final,我们就不能覆盖它。所以当我们尝试覆盖 final 方法时,

override func displayInfo() {
  print("Four Wheeler")

我们会收到一个错误消息:“error: instance method overrides a 'final' instance method”(错误:实例方法覆盖了一个“final”实例方法)。


覆盖 Swift 属性

在 Swift 中,我们可以覆盖 计算属性。例如,

class University {

  // computed property
  var cost: Int {
  return 5000
 }
}

class Fee: University {
 // override computed property
 override var cost: Int {
 return 10000
 }
}

var amount = Fee()

// access fee property
print("New Fee:", amount.cost)

输出

New Fee: 10000

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

override var cost: Int {
  return 10000 
} 

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

// access fee property
amount.cost

调用的是子类中的属性。

注意:在 Swift 中,我们不能覆盖存储属性。例如,

class A {
  // stored property
  var num = 0
}

class B: A {
  // overriding stored property 
  override var num = 2    // Error Code
}
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战