- Swift 教程
- Swift - 首页
- Swift - 概述
- Swift - 环境
- Swift - 基本语法
- Swift - 变量
- Swift - 常量
- Swift - 字面量
- Swift - 注释
- Swift 运算符
- Swift - 运算符
- Swift - 算术运算符
- Swift - 比较运算符
- Swift - 逻辑运算符
- Swift - 赋值运算符
- Swift - 位运算符
- Swift - 其他运算符
- Swift 高级运算符
- Swift - 运算符重载
- Swift - 算术溢出运算符
- Swift - 恒等运算符
- Swift - 范围运算符
- Swift 数据类型
- Swift - 数据类型
- Swift - 整数
- Swift - 浮点数
- Swift - Double
- Swift - 布尔值
- Swift - 字符串
- Swift - 字符
- Swift - 类型别名
- Swift - 可选类型
- Swift - 元组
- Swift - 断言和前提条件
- Swift 控制流
- Swift - 决策制定
- Swift - if 语句
- Swift - if...else if...else 语句
- Swift - if-else 语句
- Swift - 嵌套 if 语句
- Swift - switch 语句
- Swift - 循环
- Swift - for in 循环
- Swift - while 循环
- Swift - repeat...while 循环
- Swift - continue 语句
- Swift - break 语句
- Swift - fall through 语句
- Swift 集合
- Swift - 数组
- Swift - 集合
- Swift - 字典
- Swift 函数
- Swift - 函数
- Swift - 嵌套函数
- Swift - 函数重载
- Swift - 递归
- Swift - 高阶函数
- Swift 闭包
- Swift - 闭包
- Swift-逃逸和非逃逸闭包
- Swift - 自动闭包
- Swift 面向对象
- Swift - 枚举
- Swift - 结构体
- Swift - 类
- Swift - 属性
- Swift - 方法
- Swift - 下标
- Swift - 继承
- Swift-重写
- Swift - 初始化
- Swift - 析构
- Swift 高级
- Swift - ARC 概述
- Swift - 可选链
- Swift - 错误处理
- Swift - 并发
- Swift - 类型转换
- Swift - 嵌套类型
- Swift - 扩展
- Swift - 协议
- Swift - 泛型
- Swift - 访问控制
- Swift - 函数与方法
- Swift - SwiftyJSON
- Swift - 单例类
- Swift 随机数
- Swift 不透明类型和装箱类型
- Swift 有用资源
- Swift - 在线编译
- Swift - 快速指南
- Swift - 有用资源
- Swift - 讨论
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。
示例
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。
示例
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
混合继承
混合继承是指多个类从单个基类派生的继承。这些类可以访问和重写基类的属性和方法,并且可以拥有自己的属性和方法。例如,类 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