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