swift继承

来源:互联网 发布:java怎么使用log4j 编辑:程序博客网 时间:2024/06/05 06:03

A class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. Inheritance is a fundamental behavior that differentiates classes from other types in Swift.
一个类可以从其他类继承属性和其他的特性。当一个类继承于另一个类时,它叫做子类,自己继承的那个类叫做父类。继承是类和其他类型不同的基础。
Classes in Swift can call and access methods, properties, and subscripts belonging to their superclass and can provide their own overriding versions of those methods, properties, and subscripts to refine or modify their behavior. Swift helps to ensure your overrides are correct by checking that the override definition has a matching superclass definition.
在swift中,类可以调用和访问属于父类的方法,属性,附属脚本。也可以重写这些方法属性来改善和修改它们的行为。swift会帮助确保你的重写行为是正确的,通过检查匹配的父类。
这里写图片描述
Classes can also add property observers to inherited properties in order to be notified when the value of a property changes. Property observers can be added to any property, regardless of whether it was originally defined as a stored or computed property.
类也有属性观察机制。
基类
Any class that does not inherit from another class is known as a base class.
任何不继承自其它类被称为基类。
Swift classes do not inherit from a universal base class. Classes you define without specifying a superclass automatically become base classes for you to build upon.
swift的类不继承于一个通用的基类。你定义一个类不指定父类的话将会自动变成基类。
子类
Subclassing is the act of basing a new class on an existing class. The subclass inherits characteristics from the existing class, which you can then refine. You can also add new characteristics to the subclass.
子类是基于父类,子类从已经存在的类继承特性。并且你可以优化改善它。你也可以为子类添加新特性。
To indicate that a subclass has a superclass, write the subclass name before the superclass name, separated by a colon:
为了表明子类继承自父类,写子类的名字在父类前面用冒号分割。

0 0
原创粉丝点击