The construction of the import foundation/* class requires two stages: the first stage: 1. A constructor for the program call subclass 2. Allocates memory for the instance, The memory of the instance is not initialized at this time 3. Specifies that the constructor ensures that all instance store properties defined by the subclass are assigned an initial value of 4. Specifies that the constructor invokes the constructor of the parent class, completing the initialization of the instance store properties defined by the parent Class 5. Follow the constructor chain that invokes the parent class constructor, Until you reach the top second stage of the constructor chain: 1. Along the inheritance tree, the constructor can modify instance properties and access self at this point, and even invoke instance Method 2. Finally, the convenience constructors in the constructor chain have the opportunity to customize the instance and use self in order to make the construction process more secure, Swift Security Check 1: Specifies that the constructor must initialize the instance store properties defined in the current class before invoking the parent class constructor Security Check 2: Specifies that the constructor must call up the parent class constructor before you can assign a value to the inherited property security Check 3: The convenience constructor must first call the other constructors of the same class before you can assign a value to the property security check 4: The constructor cannot call an instance method until the first phase is complete, cannot read instance properties */class person {var name:string! var weight:double//define specified constructor init (name:string, weight:double) {self.name = name self.weight = Weight}//define convenience constructors (using convenience adornments) convenience init (n name:string, W weight:double) {self.init (NA Me:name, Weight:weight)}}class Man:person {var sex:string! Init (sex:string, name:string, weight:double) {//print (Super.name) cannot be called before the parent class is initialized before calling properties in the parent class Super.init (name:n Ame, weight:weight) super.name = "Lala" Print (self.name)//print (Self.sex) cannot be called when a property in this class is not initialized//appears: Fatal error:unexpectedly found nil while unwrapping an Optional value Error self.sex = sex print (self.sex)} convenience init (s sex:string, n name:string, weight:double) {//cannot access or modify any instance store properties//print (Self.name) errors//Super.name = name before invoking other constructors Error Self.init (Sex:sex, Name:name, weight:weight)}}var man = Mans (Sex: "Male", Name: "Rinpe", weight:62.0)
Two-segment construction of classes in Swift (class construction process)