Swift - 继承



继承是面向对象编程中最常用的特性之一,它允许一个类(子类或派生类)继承另一个类(基类或超类)的方法、属性和功能。它提供了一种组织和重用代码的机制。它也用于在类之间创建层次结构关系。Swift 还提供了访问控制修饰符来控制基类和子类中属性和方法的可见性。

在 Swift 中,类可以进一步分为子类和超类:

  • 子类 - 当一个类继承另一个类的属性、方法和函数时,它被称为子类或派生类。

  • 超类 - 一个包含属性、方法和函数的类,其他类可以从中继承,称为超类或基类。

基类

不继承其他类的方法、属性或函数的类称为基类。或者我们可以说基类是其他从中派生的类的基础类。基类也称为超类。它提供了一组可以在其子类之间共享的方法、属性和行为。基类的定义方式与普通类相同,即使用 class 关键字。

语法

以下是基类的语法:

class BaseClassName{
   // Properties
   // Methods
} 

示例

Swift 程序演示如何创建基类。

// Base class
class StudDetails {

   // Properties
   var stname: String
   var mark1: Int
   var mark2: Int
   var mark3: Int
    
   // Initializer
   init(stname: String, mark1: Int, mark2: Int, mark3: Int) {
      self.stname = stname
      self.mark1 = mark1
      self.mark2 = mark2
      self.mark3 = mark3
   }
}

// Instance of the base class
let stObj = StudDetails(stname: "Swift", mark1: 98, mark2: 89, mark3: 76)

// Accessing the Properties
print(stObj.stname)
print(stObj.mark1)
print(stObj.mark2)
print(stObj.mark3)

输出

它将产生以下输出:

Swift
98
89
76

子类

从基类或现有类创建的类称为子类。子类继承基类的方法和属性,并且还可以拥有自己的属性和方法。它还可以重写从超类继承的属性和方法。子类也可以有子类,它们继承或重写直接基类(子类)和基类的属性和方法,并且还可以拥有自己的其他属性和方法。

子类的定义是使用 class 关键字后跟子类的名称,冒号和它将从中派生的基类的名称。在子类中,我们必须在子类的初始化器中使用super.init,以确保在执行子类的初始化代码之前执行基类的初始化代码。

语法

以下是子类的语法:

class SubClassName : BaseClassName{
   // Properties
   // Methods
} 

示例

Swift 程序演示子类。

// Base Class
class StudDetails
{

   // Properties
   var name: String
   var age: Int
   var className: String  
    
   // Initializer
   init(name: String, age: Int, className: String)
   {
      self.name = name
      self.age = age
      self.className = className
   }
    
   // Method
   func Show()
   {
      print("Name: \(name), Age: \(age), className: \(className)")
   }
}

// Sub class
class Marks: StudDetails{

   // Properties
   var mark1: Int
   var mark2: Int
    
   // Initializer
   init(mark1: Int, mark2: Int, name: String)
   {
      self.mark1 = mark1
      self.mark2 = mark2
        
      // This Initializer will execute before the Initializer of sub-class
      super.init(name: name, age: 18, className: "3rd")
        
   }
    
   // Overriding method of base class
   override func Show()
   {
      print("Mark1: \(mark1) and Mark2: \(mark2)")
   }
}
// Creating an instance of a subclass
let obj = Marks(mark1: 23, mark2: 45, name: "Mohita")

// Accessing the method
obj.Show()

// Accessing the properties of the base class using the subclass instance
print("Name:", obj.name)
print("Age:", obj.age)

输出

它将产生以下输出:

Mark1: 23 and Mark2: 45
Name: Mohita
Age: 18

继承类型

Swift 支持以下类型的继承:

  • 单一继承
  • 多级继承
  • 层次继承

Swift 类不支持多重继承,以避免一些复杂性和挑战,例如菱形问题、耦合增加和初始化挑战。但是,我们可以使用协议来实现多重继承。

单一继承

单一继承是指一个类仅从一个超类派生的继承,子类可以访问超类所有属性和方法。例如,类 Y 派生自类 X。

Single Inheritance

示例

Swift 程序演示单一继承。

// Base Class
class Employee
{

   // Properties
   var name: String
   var age: Int   
    
   // Initializer
   init(name: String, age: Int)
   {
      self.name = name
      self.age = age
   }
    
   // Method
   func Show()
   {
      print("Name: \(name), Age: \(age)")
   }
}

// Subclass
class EmpDetails: Employee{

   // Properties
   var department: String
   var salary: Int
   var joiningYear: Int
    
   // Initializer
   init(department: String, salary: Int, joiningYear: Int)
   {
      self.department = department
      self.salary = salary
      self.joiningYear = joiningYear
        
      // Calling superclass initializer
      super.init(name: "Rohit", age: 23)
   }
    
   // Method 
   func Display()
   {
      print("Department: \(department), Salary: \(salary), and Joining Year: \(joiningYear)")
   }
}
// Creating an instance of a subclass
let obj = EmpDetails(department: "HR", salary: 23000, joiningYear: 2021)

// Accessing the method of superclass
obj.Show()

// Accessing the methods of the subclass
obj.Display()
输出

它将产生以下输出:

Name: Rohit, Age: 23
Department: HR, Salary: 23000, and Joining Year: 2021

多级继承

多级继承是指一个类从另一个类派生,然后另一个类从子类派生。或者我们可以说,多级继承是超过两级继承的链。例如,类 Y 派生自类 X,然后类 Z 派生自类 Y。

Multi-level Inheritance

示例

Swift 程序演示多级继承。

// Base Class
class Employee
{

   // Properties
   var name: String
   var age: Int   
    
   // Initializer
   init(name: String, age: Int)
   {
      self.name = name
      self.age = age
   }
    
   // Method
   func Show()
   {
      print("Name: \(name), Age: \(age)")
   }
}

// Sub class
class EmpDetails: Employee{

   // Properties
   var department: String
   var salary: Int
    
   // Initializer
   init(department: String, salary: Int)
   {
      self.department = department
      self.salary = salary
        
      // Calling the initializer of Employee class
      super.init(name: "Rohit", age: 23)
   }
    
   // Method 
   func Display()
   {
      print("Department: \(department) and Salary: \(salary)")
   }
}

// Another sub-class derived from EmpDetails class
class EmpWork: EmpDetails{

   var joiningYear : Int
    
   // Initializer
   init(joiningYear: Int)
   {
      self.joiningYear = joiningYear
        
      // Calling the initializer of EmpDetails class
      super.init(department: "HR", salary: 23000)
   }
    
   // Method
   func DisplayData(){
    
      // Here we are accessing the Properties of Employee and EmpDetails classes
      print("Employee Name: \(name), \nDepartment: \(department), and Joining Year: \(joiningYear)")
   }
} 

// Creating an instance of EmpWork class
let obj = EmpWork(joiningYear: 2021)

// Accessing the method 
obj.DisplayData()
输出

它将产生以下输出:

Name: Rohit, Age: 23
Department: HR, Salary: 23000, and Joining Year: 2021
Base Class

混合继承

混合继承是指多个类从单个基类派生的继承。这些类可以访问和重写基类的属性和方法,并且可以拥有自己的属性和方法。例如,类 Y 派生自类 X,然后类 Z 派生自类 X。

示例

Swift 程序演示混合继承。

// Base Class
class Employee
{

   // Properties
   var name: String
   var age: Int   
    
   // Initializer
   init(name: String, age: Int)
   {
      self.name = name
      self.age = age
   }
    
   // Method
   func Show()
   {
      print("Name: \(name), Age: \(age)")
   }
}

// Subclass derived from Employee class
class EmpDetails: Employee {

   // Property
   var salary: Int
    
   // Initializer
   init(salary: Int)
   {
      self.salary = salary
        
      // Calling the initializer of Employee class
      super.init(name: "Rohit", age: 23)
   }
    
   // Method 
   func Display()
   {
      print("Employee Name: \(name) and Salary: \(salary)")
   }
}

// Subclass derived from Employee class
class EmpWork: Employee{

   // Property
   var joiningYear : Int
    
   // Initializer
   init(joiningYear: Int)
   {
      self.joiningYear = joiningYear
        
      // Calling the initializer of Employee class
      super.init(name: "Mohina", age: 22)
   }
    
   // Method
   func Display(){
    
      // Here we are accessing the properties of Employee and EmpWork classes
      print("Employee Name: \(name) and Joining Year: \(joiningYear)")
   }
} 

// Creating an instance of a subclass
let obj1 = EmpDetails(salary: 24000)
let obj2 = EmpWork(joiningYear: 2022)

// Accessing the method of EmpDetails class
obj1.Display()

// Accessing the method of EmpWork class
obj2.Display()
输出

它将产生以下输出:

Employee Name: Rohit and Salary: 24000
Employee Name: Mohina and Joining Year: 2022
广告