iOS development language Swift Getting started serial-inheritance
A class can inherit (inherit) another class's methods (methods), property, and other attributes. When a class inherits from another class, the inheriting class is called the subclass (subclass), and the inherited class is called the superclass (or parent class, superclass). In Swift, inheritance is a basic feature of distinguishing between "class" and other types.
In Swift, classes can invoke and access the superclass's methods, properties, and subscript scripts (subscripts), and can override (override) These methods, properties, and subscripts to optimize or modify their behavior. Swift checks to see if your rewrite definition has a matching definition in the superclass to ensure that your rewrite behavior is correct.
Property observer can be added to attributes inherited from a class so that when the property value changes, the class is notified. You can add a property observer for any property, whether it was originally defined as a stored-type property (stored) or a computed property (computed).
Define a base class (base classes)
Classes that do not inherit from other classes are called base classes (base CALSS).
Attention:
The classes in are not inherited from a common base class. If you do not specify a superclass for the class you define, the class automatically becomes the base class.
The following example defines a base class called vehicle. This base class declares two properties (Numberofwheels and maxpassengers) that are common to all vehicles. These properties are used in the description method, which returns a string-type description of the vehicle's characteristics:
class Vehicle { varnumberOfWheels: Int varmaxPassengers: Int func description() -> String { return"\(numberOfWheels) wheels; up to \(maxPassengers) passengers" } init() { 0 1 }}
The Vehicle class defines the constructor (initializer) to set the value of the property. The constructor is described in detail in the construction process section, where we do a brief introduction to explain how the attributes inherited from the subclass are modified. The constructor is used to create a new instance of a type. Although constructors are not methods, they are syntactically similar. The work of the constructor is to prepare a new instance for use and to ensure that all properties in the instance have valid initialization values. The simplest form of a constructor is like an instance method without parameters, using the INIT keyword:
init() { // 执行构造过程}
If you want to create a new instance of the vehicle class, use the constructor syntax to invoke the above initializer, which is followed by an empty parenthesis after the class name:
let someVehicle = Vehicle()
The constructor for this vehicle class sets some initialization property values (numberofwheels = 0 and maxpassengers = 1) for any vehicle. The Vehicle class defines the common characteristics of a vehicle, but the class itself is not much useful. To make it more practical, you need to refine it further to describe a more specific vehicle. Subclass Generation (subclassing) subclass generation (subclassing) refers to creating a new class on the basis of an existing class. Subclasses inherit the attributes of a superclass and can optimize or change it. You can also add new attributes to subclasses. To indicate a superclass of a class, write the superclass name after the subclass name, separated by a colon:
class SomeClass: SomeSuperclass { // 类的定义}
In the next example, define a more specific vehicle class called bicycle. This new class is created on the basis of the vehicle class. So you need to put the vehicle class behind the Bicycle class, separated by colons. We can read this as: "Define a new class called bicycle, which inherits the characteristics of vehicle";
class Bicycle: Vehicle { init() { super.init() 2 }}
Previewbicycle is a subclass of Vehicle, and Vehicle is a super class of bicycle. The new bicycle class automatically gets the attributes of the vehicle class, such as the Maxpassengers and Numberofwheels properties. You can customize these features in subclasses, or add new features to better describe the bicycle class. The class defines a constructor to set its custom features (bikes only have 2 wheels). The Bicycle constructor calls the constructor Super.init () of its parent class vehicle to ensure that the vehicle class has initialized them before the Bicycle class attempts to modify those inherited properties. Note: Unlike Objective-c, in Swift, the initializer is not inherited by default, and it is already correct for the bike that the initializer inherits and overrides the default value Maxpassengers in the Vehicle class, so it is not changed in the bicycle constructor. The original value of Numberofwheels is incorrect for the bike, so change it to 2 in the initializer. Bicycle can inherit not only the properties of vehicle, but also its methods. If you create an instance of the bicycle class, you can invoke the description method it inherits from, and you can see that its output property values have changed:
let bicycle = Bicycle()println("Bicycle: \(bicycle.description())")// Bicycle: 2 wheels; up to 1 passengers
Subclasses can also continue to be inherited by other classes:
class Tandem: Bicycle { init() { super.init() 2 }}
The example above creates a subclass of bicycle: a two-person bike (tandem). Tandem inherits two attributes from bicycle, and these two properties are inherited by bicycle from vehicle. Tandem does not modify the number of wheels because it is still a bicycle, with 2 wheels. But it needs to modify the value of the maxpassengers because the two-person bike can sit two people. Note: Subclasses are allowed to modify only the variable properties inherited from the superclass, not the inherited constant properties. Create an instance of the Tandem class and print its description to see that its properties have been updated:
let tandem = Tandem()println("Tandem: \(tandem.description())")// Tandem: 2 wheels; up to 2 passengers
Note that the Tandem class also inherits the description method. An instance method of a class is inherited by all subclasses of the class. overriding (overriding) subclasses can be inherited from instance methods (instance method), class methods, instance properties (instance property), or subscript scripts (subscript) to provide their own customized implementations (implementation). We call this behavior rewriting (overriding). If you want to override an attribute, you need to precede the override definition with the Override keyword. By doing so, you have shown that you are trying to provide a rewrite version, rather than providing the same definition in the wrong way. Unexpected overrides can cause unpredictable errors, and any overrides that lack the override keyword are diagnosed as errors at compile time. The override keyword reminds the Swift compiler to check if the class's superclass (or one of its parent classes) has a claim that matches the rewritten version. This check will ensure that your rewrite definition is correct. Accessing superclass methods, properties, and subscript scripts when you override a superclass's method, property, or subscript script in a subclass, it is sometimes useful to use an already existing superclass implementation in your rewritten version. For example, you can optimize the behavior of an existing implementation, or store a modified value in an inherited variable. In the right place, you can access the superclass version of the method, property, or subscript script by using the Super prefix: in the overridden implementation of method SomeMethod, the superclass version of the SomeMethod method can be called by Super.somemethod (). In the overridden implementation of the getter or setter of the property Someproperty, the Someproperty property of the superclass version can be accessed through Super.someproperty. In the overriding implementation of the subscript script, you can access the same subscript script in the superclass version through Super[someindex]. Overriding methods in subclasses, you can override inherited instance methods or class methods to provide a custom or alternative method implementation. The following example defines a new subclass of vehicle, called Car, which overrides the description method inherited from the vehicle class:
class car : vehicle { var speed:double = 0.0 init () {super . Init () Maxpassengers = 5 numberofwheels = 4 } override func description () String {return super . Description () + "; " + "traveling at \ (speed) mph" }}
Car declares a new storage-type property speed, which is of type double and the default value is 0.0, which means "0 mph". Car has its own initializer, which sets the maximum number of passengers to 5, and the number of wheels is set to 4. Car Overrides the inherited description method, whose declaration is consistent with the description method in vehicle, with the override keyword prepended to the declaration. The description method in car is not completely customizable, but instead uses the description method in the superclass vehicle through Super.description, and then appends some additional information, such as the current speed of the car. If you create a new instance of car and print the output of the description method, you will find that the description information has changed:
let car = Car()println("Car: \(car.description())")// Car: 4 wheels; up to 5 passengers; traveling at 0.0 mph
overriding properties
You can override an inherited instance property or Class property, provide your own custom getter and setter, or add a property observer to make the overridden property observe when the property value changes. Overriding properties of getters and setters you can provide a custom getter (or setter) to override any inherited attribute, regardless of whether the inherited attribute is a stored or computed property. Subclasses do not know whether inherited attributes are stored or computed, and only know that inherited attributes have a name and type. When you rewrite a property, it is necessary to write its name and type. This allows the compiler to check that the property you are overriding matches a property of the same type in the superclass. You can rewrite an inherited read-only property as a read-write property, and you only need to provide getter and setter in the overridden version of the property. However, you cannot override an inherited read-write property to a read-only property. Note: If you provide a setter in the Override property, then you must also provide a getter. If you do not want to modify inherited property values in a getter in the rewritten version, you can return Super.someproperty directly to return the inherited value. As shown in the Speedlimitedcar example below. The following example defines a new class called Speedlimitedcar, which is a subclass of car. The class Speedlimitedcar represents a car with a speed limiting device, which can reach speeds of up to 40mph. You can implement this speed limit by overriding the slow property of the inheritance:
class SpeedLimitedCar: Car { overridevar speed: Double { get { returnsuper.speed } set { super40.0) } }}
When you set the Speed property of a Speedlimitedcar instance, the implementation of the property setter checks the size of the new value and the limit value 40mph, which sets the speed of the superclass to the smaller of NewValue and 40.0. Which of these two values is smaller is determined by the Min function, which is a global function in the SWIFT standard library. The Min function receives two or more numbers, returning the smallest of them. If you try to set the speed property of the Speedlimitedcar instance to a number greater than 40mph, and then print the output of the description function, you will find that the velocity is limited to 40mph:
let60.0println("SpeedLimitedCar: \(limitedCar.description())")// SpeedLimitedCar: 4 wheels; up to 5 passengers; traveling at 40.0 mph
overriding property observers (properties Observer)
You can add property observers to an inherited property in property overrides. In this way, when the inherited property value changes, you are notified, regardless of how the property was originally implemented. For more information about the property watcher, see Property Watcher. Note: You cannot add a property observer for inherited const-stored properties or inherited read-only computed properties. The values of these properties cannot be set, so it is inappropriate to provide them with willset or Didset implementations. Also note that you can not provide both the overridden setter and the overridden property watcher. If you want to observe the change in the property value, and you have provided a custom setter for that property, you can observe any value change in the setter. The following example defines a new class called Automaticcar, which is a subclass of car. Automaticcar means auto-stop, which automatically selects the right gear according to the current speed. The Automaticcar also provides a custom description method to output the current stop.
class AutomaticCar: Car { var1 overridevar speed: Double { didSet { 10.01 } } override func description() -> String { returnsuper" in gear \(gear)" }}
When you set the Speed property of the Automaticcar, the Didset Viewer of the property automatically sets the Gear property to select a suitable stop for the new velocity. Specifically, the property observer divides the new speed value by 10, then the nearest integer value down, and finally the value of the gear gear. For example, when the speed is 10.0, the stop is 1 and the speed is 35.0, the stop is 4:
let35.0println("AutomaticCar: \(automatic.description())"4535.0in4
Prevent rewriting
You can prevent them from being rewritten by marking methods, attributes, or subscript scripts as final, just by adding the @final attribute to the Declaration keyword. (Example: @final var, @final func, @final class func, and @final subscript) If you rewrite the final method, the property or subscript script, you will get an error at compile time. In the extension, the method that you add to the class, the property or subscript script can also be marked as final in the extension's definition. You can mark an entire class as final by adding the @final attribute (@final Class) before the keyword class, so that the class is not inherited, or a compilation error is reported.
iOS development language Swift Getting started serial---inheritance