Categories
"Hey! White?" Lesson plans

Computed properties and packaging

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:

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:

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。

如果错误使用了全局变量会发生什么呢?

不会发生什么顶多就是程序越来越卡直到卡死不响应然后重新打开罢了

实例属性

声明在类之中的变量叫做实例的属性我们之前讨论的那些都是这个家伙它声明在类当中会随着对象的创建而放在堆里知道对象被释放掉它才会随着对象一起被垃圾收集器带走

局部变量

他被声明在方法当中一般我们不需要劳心费力地来给局部变量命名——因为它的存在时间太短啦——比如循环当中的计数器也是局部变量(毕竟那也是个方法)它随着方法的调用而被创建等到方法执行完毕从桟顶被弹出那么这个变量也就消失不见了

什么是桟?我们后边的课程会讲到现在你和它握个手就好了

生效范围

不同位置的变量有不同的名字它们的功能也不尽相同全局变量是全部类和方法都能够访问到的如果你要做点什么全局状态的保存那么全局变量是个不错的选择

而实例的属性就只能被自己的类中的方法来调用别的类想要调用那是不可能的事情——至于创建实例之后……记得我们说过封装吧?

局部变量则只能被这个方法内读取的到别的方法就不行了它生效范围最小存在的时间最短一般临时的小变量都是这样


 

这节课没有作业下节课我们做个游戏玩

Original article written by Gerber drop-off:R0uter's Blog » Computed properties and packaging

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1112.html

By R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

1 reply on “Computed properties and packaging”

Leave a Reply

Your email address will not be published. Required fields are marked *