Swift's inheritance and initialization of the struct body

Source: Internet
Author: User

/*

Swift's inheritance

If you want to override the storage properties of the parent class

1. After rewriting, you can add property observers to it. Column One:

2. You can change it to a calculated attribute (but not the original function of the Parent property). Column two:

3. The parent class is read-only (get, or let), and the subclass cannot add a property observer. Column three:

If you override the computed property of the parent class

1. The parent-Class computed property is read-only and can be overridden by subclasses to add the Seter method. Column four:

Method inheritance. Column Five:

*/

Column One:

Class Person {

var name:string = "CJ"

}

Class Child:person {

Override Var name:string {

Willset {

}

Didset {

}

}

Override var name:string = "CJ" error

}

Column two:

Class Person2 {

var name:string = "CJ"

}

Class Child2:person2 {

Override Var name:string {

get {

Return "Chenhe"

}

set {

}

}

}

Column three:

Class Person3 {

var name:string {

Return "Chenhe"

}

Let Height:int = 12

}

Class Child3:person3 {

Override Var name:string {

Willset {

//

//        }

Didset {

//

//        }

//    }

Override Let Height:int {

Willset {

//

//        }

Didset {

//

//        }

//    }

}

Column four:

Class Person4 {

var name:string {

Return "Chenhe"

}

}

Class Child4:person4 {

Override Var name:string {

get {

Return "Chenhe"

}

set {

}

}

}

Column Five:

Class Person5 {

var name:string {

Return "Chenhe"

}

Func x () {

Print ("Hello")

}

}

Class Child5:person5 {

Override Func X () {

Print ("Good for You")

}

}

Run-time polymorphism

Let a:person5 = Child5 ()

A.x ()

Initialize dry two things, allocate space, assign values to members

The initialization method of structure body

struct Point {

var x:int

var y:int

}

Let p = Point violates the principle of initialization and does not assign a value to a member. One of the following can be listed:

Let p = Point (X:1, Y:2)

struct Point2 {

var x:int = 4

var y:int = 3

}

Let P2 = Point2 ()//This will not give an error, allocate space and assign values to the member

struct POINT3 {

var x:int

var y:int

Write a full constructor to initialize all the members

Init (xx:int,yy:int) {

x = XX

y = yy

}

}

Swift's inheritance and initialization of the struct body

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.