Propagation (invocation) and overriding of constructors that may fail in swift

Source: Internet
Author: User

Import foundation/* could fail the propagation of the constructor (call) 1. A constructor that might fail can call a generic constructor in the same Class 2. A generic constructor cannot call a possible failure in the same class constructor 3. In a struct, An ordinary constructor can invoke rewrite 1 of the constructor that could fail the constructor in the same struct. Subclasses can override a potentially failed constructor in the parent class with a constructor that might fail or a normal constructor 2. The generic constructor of a subclass cannot call up the parent class's possible failed constructor 3. A possible failed constructor for a subclass can call the parent class Failed constructor */class Person {var name:string = ""//Definition specified constructor init () {}//definition could fail constructor init? (name:string)        {if!name.isempty {self.name = name} else {print ("Construction failed") return nil }}}class Man:person {var sex = "male"//constructor overriding parent class with generic constructor override init (name:string) {//By The constructor is a normal constructor, so you cannot call the explicit (?) of the parent class. Constructors that may fail//can invoke the implicit (!) of the parent class. A constructor that might fail or call a normal constructor in the parent class Super.init ()}//defines a possible failure constructor init! (Name:string, sex:string)            A possible failure in the {///subclass constructor can call the parent class that could fail the constructor Super.init (name:name) if sex = = "Other" {print ("construct failed") return nil} self.sex = sex}}let p1 = man (name: "") print (p1) Let P2 = Mans (Name: "Rinpe", Sex: "Male") p Rint (P2 = = nil) Let P3 = Mans (Name: "", Sex: "Female") print (P3 = = nil) 

  

Propagation (invocation) and overriding of constructors that may fail in swift

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.