Before we in class,Take a look at the previously mentioned "SoC" concept,We say this is called "Separation of Concerns",I translate it as separation of duties - the different sections that focus on their part。Or a target to achieve a goal。
The goal not only to make the code more modular and easier to maintain,Also let the system run more efficiently。So,We want the communication between objects becomes more standardized job。
Data Hiding
You go and see you writing framework code that, when used,Which gives you direct it shows the contents inside? They are exposed to a standardized interface for you,Like one of the black box,Into the glass、There are plastic battery,A camera came out。
So,Referring to such rules,We create a class during the time,Have to do。
Would you like to find themselves on the podium just do not wear pants?
In order to ensure our code is robust enough,As many properties are marked as private (private) is a good choice。
Together we look at an chestnuts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class CellPhone { var pushedNumber = 0 //储存按了哪个按钮 func theNumberIs() ->Int{ return pushedNumber //返回按了的数字 } } var myCellPhone = CellPhone() myCellPhone.pushedNumber = 9999999 //根本没有这个数字的按钮! myCellPhone.pushedNumber = -1 //这样都行?有没有王法?! myCellPhone.theNumberIs() |
I ask who the phone can call the "-1" this number?
You write wheels,You write a program,How much of this will not necessarily robust than phone class。There are always looking for caring people like this loophole,So,We should be avoided,For example, the use of non-negative "UInt" type handle complex problems,Or we can set up a calculated property to avoid property directly manipulate classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class CellPhone { private var number:Int = 0 var pushedNumber:Int { get { return number } set(num) { if num >= 0 && num <= 9 { number = num } else { print("error!") } } } func theNumberIs() ->Int{ return pushedNumber } } var myCellPhone = CellPhone() myCellPhone.pushedNumber = 9999999 //error! myCellPhone.theNumberIs() //赋值失败,还是0 |
Swift language calculated property
In addition to property,Swift language also used to calculate the property getter and setter instead,It provides an operation can only be used to store other data itself can not attribute any variable,It is actually a method of packaging ...... summary,Thus it is actually more convenient to use。
Lifetime
All right.,Then the concept of encapsulation,We go back and store the value in the chat,we all know,Variables can be placed in a variety of places to declare,For example, outside of class,Inside class,Even inside method。Corresponding to these three positions,These variables have different meanings。
Global Variables
Declare variables outside the class called Global Variables,With this thing will run your program and on the memory of them - until you close the program,It will only be freed,So,Use of global variables to be careful,Non-essential,Please do not use。
What happens if global variables are used incorrectly?
Nothing will happen,At most, the program is getting stuck,Do not respond until stuck,Then reopen it。
Instance attributes
The variables declared in the class are called the properties of the instance,Those we discussed before are all this guy,It is declared in the class,Will be put in the heap as the object is created,Know that the object is released,It will be taken away by the garbage collector along with the object。
Local variable
He is declared in the method,Generally, we don’t need to laboriously to name the local variable-because its existence time is too short-for example, the counter in the loop is also a local variable (after all, that is also a method),It is created as the method is called,Wait until the method is executed to be ejected from the top,Then this variable disappears。
What is 桟? We will talk about,Now you can shake hands with it。
Effective range
Variables in different locations have different names,Their functions are also different。Global variables are accessible to all classes and methods,If you want to save the global state,Then global variables are a good choice。
And the properties of an instance can only be called by methods in its own class. It is impossible for other classes to call it-as for after the instance is created...Remember we talked about encapsulation, right?
Local variables can only be read by this method,Other methods will not work,It has the smallest effective range,Shortest existence,Generally temporary small variables are like this。
There is no homework for this class,Let's play a game in the next class。
Original article written by LogStudio:R0uter's Blog » Computed properties and packaging
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1112.html
Comments