Swift Builder _009-swift Constructor

Source: Internet
Author: User

//: Playground-noun:a Place where people can playImport UIKit//----constructor-------//role of the constructor: used to initialize instances of classes, structs, enumerations//If you do not explicitly define the constructor, the system automatically generates a//struct: Generates a one -by-member constructor//class: Generates an argument-free constructor, initialized with the initial value of each property//1. Default constructors for classes and structsstructWeather {var temp:double init () {temp= at}}var weather1=Weather () print (weather1.temp)classDog {Let color:string=" White"var name:string="Wong Choy"var age:int=1}let Dog=Dog () print (dog.color) print (dog.name) print (dog.age)//2. Custom ConstructorsstructCity {var name:string?var location:string?var weather:weather?Init (name:string, location:string, weather:weather) {self.name=name Self.weather=Weather Self.location=Location }//init method with external parameter nameInit (CityName name:string, _ location:string, Wea weather:weather) {Self.name=name Self.location=Location Self.weather=Weather}} var city= City (cityname:"Hangzhou","Middle", Wea:weather ()) print (city)

Swift Builder _009-swift Constructor

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.