12. Inheriting inheritance

Source: Internet
Author: User

1. Overview

Subclasses can access the parent class's methods, properties, and small-label scripts. And you can override the version of the parent class to implement your own version.

Similarly, you can provide a property monitor for the properties of the parent class (stored or computed) in the subclass.

2. Define a base class defining a base classes

Not inherited from any class, called a base class.

As below, define a car's base class:

    class Vehicle {      var0.0    varString {// read-only property    return"traveling at \ (currentspeed) miles per hour"     }    func makenoise () {        // do nothing-an arbitrary vehicle doesn ' t necessarily Make a noise     }    }      

3. Sub-class subclassing

Define a bike Class

    class bicycle:vehicle {        varfalse    }

4. Heavy-duty (overlay) overriding

Overwrite override The version of the parent class with a keyword in a subclass

5. Accessing the parent class's methods, properties, and small script accessing superclass Methods, property, and subscripts

When a subclass defines its own version, overwriting the version of the parent class, sometimes it needs to access the version of the super parent class, at which point the parent version is accessed using the keyword, such as:

super.someMethod()

super.someProperty

super[someIndex]

6. Coverage method overriding Methods

For example, define a train class:

    class train:vehicle {      override  func makenoise () {    println ("Choo choo")    }    }    = Train ()    //  prints" Choo Choo " 

7. Overriding attributes overriding properties

7.1 Override the Get method and set method overriding property Getters and Setters

You can provide a Get method and a set method to any property of the parent class in the subclass.

In the parent class for the Read-only property, you can give it a get and set method in the subclass so that it is the Read-write property in the subclass.

Conversely, you cannot read-write a property in the parent class, overwriting it as read-only in the subclass.

For example, car class:

    classcar:vehicle {varGear =1        Override vardescription:string {returnSuper.description +"In gear \ (gear)"}}  let car=Car () car.currentspeed=25.0Car.gear=3 println("Car: \ (car.description)")    //car:traveling at 25.0 miles per hour in gear 3

7.2 Override attribute monitor overriding property observers

You can add property monitors to any inherited property, except for the following:

1) You cannot add property monitors to inherited constant stored properties and inherited read-only computed properties in subclasses because they cannot be changed.

2) You can no longer provide the overridden (overloaded) Set method and the overridden (overloaded) property monitor in the subclass because you are able to react to changes in the property values in the Set method.

    classAutomaticcar:car {Override varcurrentspeed: Double { didset {gear=Int(Currentspeed/10.0) +1}}} Let automatic  =Automaticcar () automatic.currentspeed=35.0 println("Automaticcar: \ (automatic.description)") //automaticcar:traveling at 35.0 miles per hour in gear 4

8. Stop overwriting preventing Overrides

You can use the final keyword to make the properties, methods, and small script scripts in a class not covered by the quilt class, such as

final var

final func

final class func

final subscript

final class //这个类不能被任何类继承

Reference: Https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_ language/inheritance.html#//apple_ref/doc/uid/tp40014097-ch17-id193

12. Inheriting inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.