Swift to succeed

Source: Internet
Author: User

Everyone should be familiar with the name of inheritance, as in Swift, a class in Swift can inherit the methods, properties, and other attributes of another class. When a class inherits from another class, the inheriting class is called a subclass, the inherited class is called the parent class, and inheritance in Swift is a basic feature of the zone classification and other types.

Define a base class:

When a class does not inherit other classes, this class is called the base class. For example, look at the following code:

Class Vehicle {    var maxpassengers:int    var manufacturer:string        func description ()-string{        return "Max passengers is \ (maxpassengers) and manufacturer is \ (manufacturer)"    }        init () {        maxpassengers = 2        ma Nufacturer = "Designed by Weasley"    }    }

We define a base class: vehicle, which defines 2 properties, one maximum number of passengers: Maxpassengers, one manufacturer: Manufacturer, one Init constructor, and one description function. The value that the constructor uses to set the property to create a new instance of a type. I'll talk about this later.

To generate subclasses:

Subclasses are creating a new class based on an existing class that inherits the characteristics of the parent class, and can optimize and alter these characteristics, and of course add new unique features to the subclass.

The subclass is defined in the following format:

Class < #name #>: < #super class#> {    < #properties and Methods#>}

The colon is preceded by the subclass name, followed by the parent class name, and the function body can inherit the optimization and change the parent class's related properties or methods.

For example, we define a bicycle class that inherits from the vehicle class:

<pre name= "code" class= "OBJC" >class bicycle:vehicle {    var numberofwheels:int = 2    var maxspeed:double = -        override Init () {        super.init ()        maxpassengers = 2    }        override func description (), String {        return "Numberofwheels: \ (numberofwheels)  max passengers: \ (maxpassengers) with Max speed is \ (maxspeed) manufa Cturer is \ (manufacturer) "    }}

Now that our bicycle subclass has been created, bicycle is the subclass of vehicle, which contains all the features of vehicle, the maximum number of passengers, the manufacturer, etc.

We have added 2 new attributes to the subclass, one for Numberofwheels, one for maxspeed, for overriding the constructor init function, and for overriding the description function, and it's worth noting that when we rewrite some functions in the parent class, You need to precede the function name with the override keyword and the no-word system compiler will error.

This time we create an instance of bicycle to see if some of our values are correct:

var abicycle = Bicycle () println (Abicycle.description ())

We have the following output:

Numberofwheels:2  Max Passengers:2 with Max speed is 30.0 manufacturer are designed by Weasley

We can see that the subclass inherits some of the properties of the parent class.


To access the properties or methods of the parent class:

When you override a superclass's method, property, or satellite script in a subclass, sometimes you use an existing one in your rewritten version
Super-class implementations can be useful. 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 a method, property, or satellite 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 overridden implementation of a subordinate script, you can access the same satellite script in the superclass version through Super[someindex].

For example, we can rewrite just the bicycle class:

Class Bicycle:vehicle {    var numberofwheels:int = 2    var maxspeed:double =        override init () {        super.i NIT ()        maxpassengers = 2    }        override func description (), String {        return super.description () + ";" + "Number of wheels \ (numberofwheels) Max speed \ (maxspeed)}    }

Looking at the description function in the bicycle class, this function overrides the description function of the vehicle class and uses super.description to get the description function of the parent class. The resulting result is this:

Max passengers is 2 and manufacturer are designed by Weasley;number of Wheels 2 Max speed 30.0

Overriding properties:

You can override inherited instance properties or class properties to provide custom getter and setter methods:

Note: If you write the setter in the override attribute, you must also write the 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.

Class Limitspeedbicycle:bicycle {    override var maxspeed:double{        get{            return super.maxspeed        }        set{            super.maxspeed = min (maxspeed, h)}}}    


This overrides the Maxspeed property, and when we set the Maxspeed property, it returns a minimum value compared to 20.
Max passengers is 2 and manufacturer are designed by Weasley; Number of wheels 2 max speed 20.0


To override a property watcher:

We can add a property observer to an inherited property in the property override, so that when the property changes, we can do it accordingly.
Note: We cannot add property observers for read-only and constant properties, because constants and read-only properties cannot be set, so it is not possible to provide him with Willset and Didset methods, as shown in the following example:
Class Speedlevel:bicycle {    var level  = 1    override var maxspeed:double{        didset{Level            = Int (maxspee D/10) + 1        }    }        override func description (), String {        return super.description () + "level is \" ) "    }}var abicycle = Speedlevel () abicycle.maxspeed = 40println (Abicycle.description ())


We get the following output:
Max passengers is 2 and manufacturer are designed by Weasley; Number of wheels 2 Max speed 40.0 level is 5


So the question comes, what if I don't want my property or method to be rewritten?
The final keyword can help you solve this problem: final var final class final Func

Welcome all to guide the study together.

Swift to succeed

Related Article

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.