Swift classes and structures, storage properties, calculation properties, functions and methods, satellite scripts, and so on

Source: Internet
Author: User

I. CLASS and Structure body

The two are not much worse in Swift.

There are many similarities between a class and a struct: (2nd, 3 points are not present in other languages)

1 can define the attribute/method/subscript (the structure can also define the method)

2) can be initialized (through the construction method)

3) can use extension (extension) and Protocol (protocol)

4 Global Properties/methods in classes and structs (enumerations): Class keywords in classes, but static in structs


Analogy structure many functions:

1) can Inherit

2 The runtime can check the type of class object

3) destructor Release resources

4 reference count allows a class instance to have multiple references

[Java] view plain copy class person1{var name = "Jack"//class var height:int = 10//Error! Class cannot have global The property can only be stored as a computed property.   See the following description} struct person2{//struct is more like class Var age:int//static var height:int in Java = 10//can set global properties that need to be initialized. //1.1 instantiate class and struct, default constructor let P1 = Person1 () Let P2 = Person2 (age:10)//when using default constructor, all attributes must be initialized


Properties (Storage Properties--Computed properties--class properties)

2.1 Storage properties: is to store a constant or variable, similar to the member variables in Java [Java] view plain copy class person3{var name:string = "Xuneng"//Need To manually initialize the var age:int = ' let ' height = 175 lazy var p1:person1 = Person1 ()//latency, must be var} var p3 = Person3 () P3.name//through point syntax to access P3.age = 11//set//Latency Lazy Storage properties: Initialize when using the modifier, the benefit is to avoid space waste println (P3.P1)///This sentence When the P1 is initialized

2.2 Compute properties: Do not store the value directly, but do it with get/set. Can be used to manipulate changes to other property values

Get: To encapsulate the process of taking a value

Set: Set to encapsulate the process of setting a value

[Java]   View plain  copy   class person4{        var name:String =  "Jack"        var jjlength:int =  10       var age:int{  //can only be var            get{                return jjLength * 2            }           set (newage) {                jjLength = newAge / 2  

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.