ios swift 继承_Swift中的继承

ios swift 继承

什么是继承? (What is Inheritance?)

Inheritance allows a class to have the same behavior as of another class and extend that behavior to provide specific needs.

继承允许一个类具有与另一个类相同的行为,并扩展该行为以提供特定的需求。

为什么要继承? (Why Inheritance?)

One of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing common code amongst several subclasses.

继承的主要好处之一是,通过在多个子类之间共享通用代码,可以最大程度地减少应用程序中重复代码的数量。

When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its base class.

当一个类从另一个类继承时,继承的类称为子类 ,而其继承的类称为其基类

Inheritance is a fundamental behavior that differentiates classes from other types in Swift.

继承是一种基本行为,可将Swift中的类与其他类型区分开。

Topics Covered are,

涵盖的主题是

  1. Base Class

    基类
  2. Sub Class

    子类
  3. Overriding

    覆写
  4. Preventing Overriding

    防止覆写

1.基础类 (1. Base Class)

Any class that does not inherit from another class is known as a base class.

任何不从另一个类继承的类都称为基类

句法 (Syntax)

class ClassName {
//Declaration of properties and methods
}

(Example)

2.子类 (2. Sub Class)

The subclass inherits characteristics(i.e. Methods and Properties) from the existing class. New characteristics can also be added to the subclass.

子类从现有类继承特征(即方法和属性)。 新特性也可以添加到子类中。

句法 (Syntax)

class SubClassName: BaseClassName {
//Sub Class Declaration
}

(Example)

An important concept in the subclass is overriding. Let's get into that.

子类中的一个重要概念是重载。 让我们开始吧。

3.覆写 (3. Overriding)

To provide an own custom implementation in a subclass overriding is used.

为了在子类中提供自己的自定义实现,将使用重写。

Overriding is something changing or adding the functionality which is derived from the base class without changing its signature(without changing the method name and the parameter list).

重写是指更改或添加从基类派生的功能,而无需更改其签名(无需更改方法名称和参数列表)。

句法 (Syntax)

class SubClassName: BaseClassName {
override var or func
}

(Example)

  • In the above example, the Manager class is inherited from the Employee class.

    在上面的示例中,Manager类是从Employee类继承的。
  • The manager class adds an extra variable called the bonus.

    管理器类添加了一个额外的变量,称为bonus

  • Then the base class initializer of Employee is called from the Manager class.

    然后,从Manager类调用Employee的基类初始化程序。
  • Then base class method printDetails is called from the Manager class printDetails method.

    然后,从Manager类的printDetails方法调用基类方法printDetails。
  • Finally, Also the base class variables firstName and lastName are called from the method.

    最后,还从该方法调用基类变量firstName和lastName。

4.防止超越 (4. Preventing Override)

A method or property can be prevented from being overridden by marking it as final.

通过将方法或属性标记为final可以防止方法或属性被覆盖。

Any attempt to override a final method or a property in subclass results in a compile-time error.

任何尝试覆盖子类中的final方法或属性的尝试都会导致编译时错误

Not only the method or property even a class can be marked as final.

不仅方法或属性,甚至类也可以标记为final。

Any attempt to inherit the final class results in a compile-time error.

继承最终类的任何尝试都将导致编译时错误。

防止财产超支 (Preventing Property Override)

This prevents the property from being overridden.

这样可以防止该属性被覆盖。

句法 (Syntax)

class ClassName {
final var: Type
}

(Example)

防止方法覆盖 (Preventing Method Override)

This prevents the method from being overridden

这样可以防止方法被覆盖

句法 (Syntax)

class ClassName {
final func functionName() {
//Function Definition
}

(Example)

防止类覆盖 (Preventing Class Override)

For methods and properties, the final keyword doesn't allow them to be overridden. In the case of a class it prevents it from being extended.

对于方法和属性, final关键字不允许它们被覆盖。 对于类,它阻止了它的扩展

句法 (Syntax)

final class ClassName {
//Class Declaration
}

(Example)

So far concepts on the base class, subclass, overriding, and preventing overriding are covered.

到目前为止,已经涵盖了有关基类,子类,覆盖和防止覆盖的概念。

翻译自: https://medium.com/ivymobility-developers/inheritance-in-swift-5656886d41b7

ios swift 继承

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值