Introduction to Using Objects

An object is a way of organizing data and functionality in something we can use as programmers, Hence the term, object oriented programming. As an iOS developer, you'll definitely want to know about objects. And in this session we'll practice identifying them in the real world.

Object 可讓工程師用於 organizing data 跟 functionality。 。身為 iOS developer 你必須了解 object,本節課我們會開始認識 object。

When you run the Maze application, there is a lot more going on than meets the eye. Behind the scenes, the Maze app consists of a larger system of interconnected objects.

當你 run Maze 這個 app 時,其實有很多東西在背後運行,整個 app 是由很多 object 互相連結而成。

You had a chance to interact with one object already—the Robot object—and you sent it messages to move around the maze.

你有機會先跟 Robot object 這個 object 先做互動,你可以傳給他一些訊息讓 app 內的機器人移動。我們可以從下圖看到 Robot object 的 data 跟 functionality。

Objects like the Robot allow us to group related pieces of data. Once the data is grouped, we can more easily interact with it. For example, with the Robot object we can store how the robot looks and operates. Then, to interact with it, we send it instructions such as moveUp and moveDown. We’ll talk more about objects later, but for now remember that objects are capable of encapsulating data (“what it is composed of”) and functionality (“how it operates”).

Robot 這個 object 會儲存 robot 的外觀跟操作,如果我們要跟他互動,我們會送一些 instructions 像 moveUp 和 moveDown。接下來我們會講更多跟 object 相關的。現在只要知道 object 會包含 what it is composed of 跟 how it operates 即可。

In addition to the Robot object, the Maze app creates another special kind of object called a struct. Specifically, each time you sent instructions to the Robot object, a Move struct was created. When the robot is instructed to move, it creates a Move struct which directs its movement.

除了 Robot object 之外,Maze app 有一種特別的 object 類型叫做 struct,每一次送出指示到 robot object 都會有一個 Move struct 產生。

A struct (short for “structure”) is an object, like the Robot, that can encapsulate data and functionality. However, objects like the Robot tend to have more functionality than structs, which tend to be used primarily to store data. In the Maze app, the Robot interacts with the Move structs to carry out its various functions. But, the Move struct doesn’t have any real functionality of its own.

Struct 是 structure 的簡稱,是一個 object,一樣可以 encapsulate data 和 functionality。然而 robot object 比 struct 有較多的 functionality,主要用於儲存資料。Robot 執行 function 時 move struct 也一起執行,但 move struct 沒有他自己的 function。

As its name suggests, a Move struct is used by the Maze app to tell the robot object how to move. For example, a Move struct can contain data to move the robot one space to the left. It could also contain data to move the robot one space up or down.

move struct 會告訴 robot object 怎麼移動,例如怎麼向左移動一格,怎麼向上向下移動,move struct 會有這些資料。

A Move struct models a change in the robot's position.

We will talk more about the Move struct later, but what is important to realize is that the maze, the robot, and the moves are all objects. And, as you write code and build applications, you’ll find that many problems can be modeled with objects.

後面會講解更多關於 Move struct 的細節,現在先知道 maze、robot 跟 move 都是 objects 就好。

results matching ""

    No results matching ""