swift-default constructor-standby

Source: Internet
Author: User

Instances of structs and classes call a special Init method, called a constructor, during construction. The constructor does not have a return value and can be overloaded. In the case of multiple constructor overloads, the runtime can invoke the appropriate constructor based on its external parameter name or parameter list.
Default constructor
Structs and classes call a constructor during construction, and even if no constructors are written, the compiler provides a default constructor. Here's the sample code:

 
  1. Class Rectangle {
  2. var width: Double = 0.0
  3. var height: Double = 0.0
  4. }
  5. var rect = Rectangle ()//Create instance and invoke default constructor init ()
  6. Rect.width = 320.0
  7. Rect.height = 480.0
  8. Print ("Rectangle: \ (rect.width) x \ (rect.height)")
  9. Rectangle () means that a method is called, which is the default constructor, Init ().
  10. In fact, the constructor is omitted during the definition of rectangle, which is equivalent to the following code:
  11. Class Rectangle {
  12. var width: Double = 0.0
  13. var height: Double = 0.0
  14. Init () {
  15. }
  16. }

If rectangle is a struct, it is defined as follows:
struct Rectangle {
var width:double = 0.0
var height:double = 0.0
}
The default constructor for struct rectangle is different from the default constructor for class rectangle, which is equivalent to the following code:

 
  1. struct Rectangle {
  2. var width: Double = 0.0
  3. var height: Double = 0.0
  4. Init () {
  5. }
  6. Init (width:double, height:double) {//parameter-constructor
  7. self.width = width
  8. self.height = height
  9. }
  10. }

Which constructor to invoke is determined by the parameter name and the type of argument passed.

You can use the constructor proxy in a constructor to help complete some of the construction work. Class constructor proxies are divided into horizontal and upward proxies, which can only occur inside the same class, and this constructor is called a convenience constructor. An up-agent occurs in the case of inheritance, in which the parent class constructor is called to initialize the stored property of the parent class, which is called the specified constructor, during the child class construction.  

Constructor Call rules

 
  1. Examples of person and student classes:
  2. Class Person {
  3. var name:string
  4. var age:int
  5. Func description ()-> String {
  6. Return "\ (name) age is: \ (aged)"
  7. }
  8. Convenience init () {//convenience constructor
  9. Self.init (Name: "Tony")
  10. self.age =
  11. }
  12. Convenience init (name:string) {//convenience constructor
  13. Self.init (Name:name, age:18)
  14. }
  15. Init (name:string, Age:int) {//Specify constructor
  16. self.name = name
  17. self.age = Age
  18. }
  19. }
  20. Class Student:person {
  21. var school:string
  22. Init (name:string, Age:int, school:string) {//Specify constructors
  23. Self.school = School
  24. Super.init (Name:name, Age:age)
  25. }
  26. Convenience override Init (name:string, Age:int) {//convenience constructor
  27. Self.init (Name:name, Age:age, School: "Tsinghua University")
  28. }
  29. }
  30. Let student = student ()
  31. Print ("Student: \ (student.description ())")

Calls between constructors form a chain of constructor functions.

Swift restricts the rules for proxy calls between constructors to have 3, as shown below.

Specifies that the constructor must call the specified constructor of its immediate parent class. Visible from the diagram,the ④ number in student specifies that the constructor calls the ③ number in the person specified by the constructor.

      • The convenience constructor must call other constructors defined in the same class. As seen from the diagram,the ⑤ in student facilitates constructors to call the ④ in the same class to facilitate constructors, andthe ① number in person facilitates constructors by calling the ② number in the same class.

      • The convenience constructor must eventually end with a call to a specified constructor function. From the diagram, the⑤ number in student facilitates the constructor to call the ④ number in the same class to specify the constructor, andthe ② number in the person facilitates the constructor to call the ③ number in the same class to specify the constructor.

  • ==================================================
  • There are two sources of child class constructors in Swift: writing and inheriting from the parent class. Not all constructors of the parent class can inherit, and constructors that inherit from the parent class are conditional, as shown below.

      • Condition 1: If the subclass does not define any of the specified constructors, it automatically inherits the specified constructors for all the parent classes.

      • Condition 2: If the subclass provides all of the parent class to specify the implementation of the constructor, whether inherited through condition 1, or by writing it yourself, it will automatically inherit all the convenience constructors of the parent class.

    Here's the sample code:

     
    1. Class Person {
    2. var name:string
    3. var age:int
    4. Func description ()-> String {
    5. Return "\ (name) age is: \ (aged)"
    6. }
    7. Convenience init () {
    8. Self.init (Name: "Tony")
    9. self.age =
    10. }
    11. Convenience init (name:string) {
    12. Self.init (Name:name, age:18)
    13. }
    14. Init (name:string, Age:int) {
    15. self.name = name
    16. self.age = Age
    17. }
    18. }
    19. Class Student:person {
    20. var school:string
    21. Init (name:string, Age:int, school:string) {
    22. Self.school = School
    23. Super.init (Name:name, Age:age)
    24. }
    25. Convenience override Init (name:string, Age:int) {
    26. Self.init (Name:name, Age:age, School: "Tsinghua University")
    27. }
    28. }
    29. Class Graduate:student {
    30. var Special: String = ""
    31. }

    To see the inheritance of Condition 1,graduate inherits Student, and thegraduate class does not define any of the specified constructors, it automatically inherits all the Student specified constructors. After qualifying 1,graduate inherits from student the following specified constructor:

    Init (name: string, age: int,school: string)

    Again, with the inheritance of condition 2, because graduate implements all the specified constructors for student,graduate automatically inherits all the student convenience constructors. After qualifying 2,graduate inherited the following 3 convenient constructors from student:

    Init (name: String, age: Int)

    Init (name: String)

    Init ()

    Student has 4 constructors after inheriting a person.

    Condition 1 does not satisfy the student because it has the specified constructor, andthe convenience constructor Init (name:string, Age:int) in the student class satisfies condition 2, which implements the parent class to specify the constructor Init (name:string, Age:int). In addition, because the subclass constructor is the same as the parent class constructor parameter, you need to use the override keyword to represent the subclass constructor override (overriding) of the parent class constructor.

    Because the student class implements the parent class to specify the constructor, it also inherits two other convenient constructors for the parent class.

swift-default constructor-standby

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.