Learn Swift--inheritance

Source: Internet
Author: User

Inheritance

A class can inherit another class's methods (methods), attributes (properties), and other attributes. When a class inherits from another class, the inheriting class is called a subclass, and the inherited class is called the superclass (parent class).

In Swift, subclasses can invoke and access the parent class'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 observers can be added to attributes inherited from subclasses, 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).

sub-class generation
Class vehicle{      //When a class does not inherit any class defaults to the base class    var currentspeed = 0.0    var discription:string {        return "traveling at \ (currentspeed) miles per hour "    }    func makenoise () {        //do something    }}//The above code generates a transport base class under which to generate subclass class bicycle:vehicle {    //colon followed by the name of the parent class can inherit the parent class    var hasbasket = false}//First Look at the parent class let somevehicle = Vehicle () Somevehicle.currentspeed = 15.0print ("Somevehicle: \ (somevehicle.discription)")//Print out: "Somevehicle:traveling at 15.0 Miles per hour\n "//again see below class let Somebicycle = Bicycle () somebicycle.currentspeed = 20.0somebicycle.hasbasket = Trueprint (" Somebicycle: \ (somebicycle.discription) ")//Print out:" somebicycle:traveling at 20.0 miles per hour\n "//subclasses can also call the parent class in addition to their own properties Sex and methods these are inherited traits.

rewrite

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)

overriding Method
Class Train:vehicle {    //If you want to override the attributes of a parent class, you must precede func with the override    override Func Makenoise () {        print ("Ku-ku-ku ku ...") )    }}let train = Train () train.makenoise ()

Overriding Properties

Note: Subclasses can override read-only properties of the parent class as read-write properties, but must never override read-write properties of the parent class as read-only properties.

Class Car:vehicle {    var gear = 1    //discription is a read-only attribute in the base class but can override this property in subclasses and add Setter method    override Var Discriptio n:string{        set {            print (newvalue)        }                get {            return super.discription + "in the Gear: \ (gear)"            // Super represents the base class's meaning call Super.discription will return a string and then stitch the string into something you want to return as a Get}}}    

overriding property observers

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 willSet or didSet implement them. 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.

Class Automaticcar:car {    override var currentspeed:double {        Didset {            gear = Int (currentspeed/10.0) + 1
   
    }        //rewrite currentspeed Add the attribute observation period so that when Currentspeed is assigned you can do something you want to do    }
   

rewrite subscript script

Because overriding the subscript script is similar to overriding the property, there is no more introduction.

Class someclass{    subscript (index:int), Int {        set {                    }                get {            return 0        }    }}class Subclass:someclass {    override subscript (Index:int), Int {        set {            print (newvalue)        }                get { Return '}}}    

Prevent rewriting

If you declare a class and there are some attributes of the class that you do not want to override, you can use the final keyword to prevent rewriting

Class Car:vehicle {    final var gear = 1    final func brake () {        gear = 0        currentspeed = 0.0    }    //plus fin The AL keyword does not successfully override these attributes if a class inherits from this class}

Learn Swift--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.