A lesson on our first taste OOP Style,Ambiguous classes and objects in exactly what is it? This lesson we use a simple little chestnut to introduce to you。
Trying to explain
We say that the relationship between classes and objects and the relationship between the design of products,Take Our house is,Certainly there will be a corresponding building design,But the design will not only correspond to a building,At least one cell must be met with a set of drawings is not it? Our "Class" is the "design"。We use this design designed object's properties、Function and so a series of content,Then to produce objects of this class by instantiating。
For example, we created a "car" category,Then we can instantiate a different object,For example, a car truck,Car is car。
Guess a small program
We used to use a specific small program like you explain the difference between classes and objects - a main game class,Guess a player class:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
// // GuessGame.swift // test // // Created by R0uter on 15/8/2. // Copyright © 2015年 R0uter. All rights reserved. // import Foundation class GuessGame { var p1 = Player() var p2 = Player() var p3 = Player() // 三个玩家的实例 func startGame() { var guessP1 = 0 var guessP2 = 0 var guessP3 = 0 // 储存玩家猜的数字 var p1IsRight = false var p2IsRight = false var p3IsRight = false // 保存玩家是否猜中 let targetNumber = Int (arc4random_uniform(10)) // 生成一个0到9的随机数 print("生成0 - 9的随机数……") while (true) { print("要猜的数字是 \(targetNumber)") p1.guess() p2.guess() p3.guess() // 让三个玩家进行猜测,分别调用他们的 guess() 方法。 guessP1 = p1.number print("玩家1 猜测数字是 \(guessP1)") guessP2 = p2.number print("玩家2 猜测数字是 \(guessP2)") guessP3 = p3.number print("玩家3 猜测数字是 \(guessP3)") // 取出他们猜测的数字 if ( guessP1 == targetNumber) { p1IsRight = true } if ( guessP2 == targetNumber) { p2IsRight = true } if ( guessP3 == targetNumber) { p3IsRight = true } // 进行判断看他们是否猜对,如果猜对则修改对应玩家的属性 if (p1IsRight || p2IsRight || p3IsRight) { print("结果出来了") print("P1 win? \(p1IsRight)" ) print("P2 win? \(p2IsRight)") print("P3 win? \(p3IsRight)") print("GG") break } else { print("没人猜中,再来一次!") } } } } |
The next is the player's class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// // Player.swift // test // // Created by R0uter on 15/8/2. // Copyright © 2015年 R0uter. All rights reserved. // import Foundation class Player { var number = 0 //猜的数字 func guess() { number = Int (arc4random_uniform(10)) print("\(number) ?") } } |
Both classes get,However, the compiler found no movement - it does not matter,Because we have not told the inlet system program。
Find the main file,Come on we instantiate the main game:
1 2 3 4 5 6 |
import Foundation let game = GuessGame() //实例化游戏主体,生成一个游戏的对象 game.startGame() //执行对象中的方法启动游戏 |
Such,Probably there will be the following results after running - because we generate random numbers,Therefore, implementation of the results will be exactly the same every time。
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
生成0 - 9的随机数…… 要猜的数字是 9 1 ? 1 ? 3 ? 玩家1 猜测数字是 1 玩家2 猜测数字是 1 玩家3 猜测数字是 3 没人猜中,再来一次! 要猜的数字是 9 4 ? 2 ? 6 ? 玩家1 猜测数字是 4 玩家2 猜测数字是 2 玩家3 猜测数字是 6 没人猜中,再来一次! 要猜的数字是 9 8 ? 3 ? 4 ? 玩家1 猜测数字是 8 玩家2 猜测数字是 3 玩家3 猜测数字是 4 没人猜中,再来一次! 要猜的数字是 9 0 ? 1 ? 3 ? 玩家1 猜测数字是 0 玩家2 猜测数字是 1 玩家3 猜测数字是 3 没人猜中,再来一次! 要猜的数字是 9 0 ? 7 ? 4 ? 玩家1 猜测数字是 0 玩家2 猜测数字是 7 玩家3 猜测数字是 4 没人猜中,再来一次! 要猜的数字是 9 1 ? 7 ? 0 ? 玩家1 猜测数字是 1 玩家2 猜测数字是 7 玩家3 猜测数字是 0 没人猜中,再来一次! 要猜的数字是 9 1 ? 4 ? 4 ? 玩家1 猜测数字是 1 玩家2 猜测数字是 4 玩家3 猜测数字是 4 没人猜中,再来一次! 要猜的数字是 9 4 ? 0 ? 3 ? 玩家1 猜测数字是 4 玩家2 猜测数字是 0 玩家3 猜测数字是 3 没人猜中,再来一次! 要猜的数字是 9 7 ? 7 ? 4 ? 玩家1 猜测数字是 7 玩家2 猜测数字是 7 玩家3 猜测数字是 4 没人猜中,再来一次! 要猜的数字是 9 3 ? 9 ? 6 ? 玩家1 猜测数字是 3 玩家2 猜测数字是 9 玩家3 猜测数字是 6 结果出来了 P1 win? false P2 win? true P3 win? false GG Program ended with exit code: 0 |
summary
You see,We are at GuessGame Class players actually generates three objects,But in fact we are all generated from the same class,We put three players object called "Example”,They are Player This class generated,But running them in actual,Due to their different respective properties,But not exactly the same - that isObjectsAndclassDifference。
After-school title
Here is a code file is corrupted,We also recorded the results of running at the time of the output,Now you need to put it filled。
Remember that each line can only write a code yo ~ otherwise even cheating!
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 27 28 29 30 |
import Foundation class //这里丢失一行 { var //这里丢失一行 = 0 func //这里丢失一行 { print("hellooooo.....") } } var e1 = Echo() //这里丢失一行 var x = 0 while //这里丢失一行 { e1.hello() //这里丢失一行 if x == 4(还是 x == 3? 看不清楚了) { e2.count = e2.count + 1 } if x > 0 { e2.count = e2.count + e1.count } x = x + 1 } print(e2.count) |
Here are the results after the execution of code:
1 2 3 4 5 6 |
hellooooo..... hellooooo..... hellooooo..... hellooooo..... 10 Program ended with exit code: 0 |
……correct,If the result is not 10,But 24,That code is how should be amended?
1 2 3 4 5 6 |
hellooooo..... hellooooo..... hellooooo..... hellooooo..... 24 Program ended with exit code: 0 |
Original article written by LogStudio:R0uter's Blog » Guess the number game:An object of the class difference
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1108.html
import Foundation
class Echo {
var count = 0
func hello() {
print(“hellooooo…..”)
}
}
var e1 = Echo()
var e2 = Echo()
var x = 0
while x < 4 {
e1.hello()
e1.count += x
e2.count = e2.count + e1.count
x += 1
}
print(e2.count)