Some students did not take into account the iPhone,Swift language learning but at least we will have OS X operating system,Our code shows will be completed in OS X,Using the CLI interface。So although it looks old and was,But believe me take the GUI make you worry a lot - because that is another matter。
So now,Let's say hello and OOP
We take a look,Developing with OOP in the end is what it looks like?
We are not taking into account the high level of development,Then we go to all the functions implemented in place of the output text,Merely to demonstrate the "development" process itself。
Boss appeared
"This time we do a cell phone!"
Ok,We have to develop a mobile phone:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class CellPhone { func call() { print("Call someone!") } func powerUp () { print("Welcome!") } func powerDown() { print("Goodbye~") } func sendMessage() { print("sent a Message!") } } |
You see,Our phone system has four methods can be used?
"Our second-generation products to be available on the Internet!"
Ok,We then developed a cell phone out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class CellPhoneG2 { func call() { print("Call someone!") } func powerUp () { print("Welcome!") } func powerDown() { print("Goodbye~") } func sendMessage() { print("sent a Message!") } func internet() { print("Hello world!") } } |
"Our three generations of products have Wifi!"
Ok,After all, the boss who is giving you money,We go again, right:
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 CellPhoneG3 { func call() { print("Call someone!") } func powerUp () { print("Welcome!") } func powerDown() { print("Goodbye~") } func sendMessage() { print("sent a Message!") } func internet() { print("Hello world!") } func wifi() { print("free wifi~") } } |
"You can go home。”
Ok,More than three phone code no matter to whom will read worried - redundant portions too much - we have done so much wasted effort! No wonder the boss will quarrel and finally got the ax for the final salary。
This time the sky just listen to bang Duang,OOP drive to,Let's look at how to save the object-oriented field:
Abstract superclass
We say that three generations have turned off the phone calling and texting functions,Then we developed a class,They abstracts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class CellPhone { func call() { print("Call someone!") } func powerUp () { print("Welcome!") } func powerDown() { print("Goodbye~") } func sendMessage() { print("sent a Message!") } } |
"You do that and then the eggs!" (Do not worry boss)
With this foundation,We subclasses from CellPhone,Each subclass has all the functions of the parent class,We just add extra features like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class G1:CellPhone { } class G2:CellPhone { func internet() { print("Hello world!") } } class G3:CellPhone { func internet() { print("Hello world!") } func wifi() { print("free wifi~") } } |
Check it out? This will save a lot of duplication of development time!
"Do not get too excited,I would also like to develop a tablet! "
We all know that in fact is a large tablet phone,Then we only need to add tablet functions just fine on the phone,A change in hardware on ok ~
"I do not want a tablet instead of a phone's location,Their access to the Internet but can not make calls and send text messages! "
Ok,We calling and texting functions rewrite:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class GPad:CellPhone { override func call() { print("Can not Call!") } override func sendMessage() { print("Can not Send!") } func GPadModel() { print("Im not CellPhone!") } } |
All right,Now the birth of the tablet。
"Humph,I want all of the devices can be turned on with the sound! "(This is a vindictive boss you ah ~ ~)
But no,Do not roll up your sleeves and go to war - reconstruction of the entire system is not necessary。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class CellPhone { func call() { print("Call someone!") } func powerUp () { print("play music~") print("Welcome!") } func powerDown() { print("Goodbye~") } func sendMessage() { print("sent a Message!") } } |
All right,We just changed the startup of the parent class,All subclasses will have this feature。
summary
Ok,Overall, our chestnuts are not very appropriate,But it does not prevent us to understand the connotation of OOP is not it? Through mechanisms such as inheritance and rewriting of OOP,We can very quickly build a superstructure based on previous base。But also specifies the type of,We can also easily modify the previous code without affecting the subsequent code。
In particular, it is time to develop in people,OOP is to save the life of the program ape,We can be more convenient to use someone else has developed written wheels (library),You may or may not continue to develop in a certain time。
but,What classes and objects in the end what is it? Inheritance exactly how to run it? We come to understand the next lesson。
Original article written by LogStudio:R0uter's Blog » Our test environment and has shown signs of OOP
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1107.html
Easy to understand