您的位置:首页 > 移动开发 > Objective-C

An object has an interface (对象的接口)

2015-06-08 15:01 519 查看
Aristotle was probably the first to begin a careful study of the concept of type; he spoke of "the class of fishes and the class of birds." 亚里士多德或许是认真研究“概念的第一人,”, 他曾谈及“鱼类和鸟类”的问题。

The idea that all objects, while being unique, are also part of a class of objects that have characteristics and behaviors in common was used directly in the first object-oriented language, Simula-67, with its fundamental keyword class that introduces a
new type into a program. 在世界首例面向对象语言Simula-67中,第一次用到了这样的概念:所有对象--尽管各有特色--都属于某一系列对象的一部分,这些对象具有通用的特征和行为。在Simula-67中,首次用到了class这个关键字,它为程序引入了一个全新的类型。

Simula, as its name implies, was created for developing simulations such as the classic "bank teller problem." Simula,正如其名,它的目的是开发模拟像“银行出纳员”这样的经典问题。

In this, you have numerous tellers, customers, accounts, transactions, and units of money - a lot of "objects." 在这个例子中,你有一系列出纳员、客户、账号、交易以及金钱 -- 这许多对象。

Objects that are identical except for their state during a program's execution are grouped together into "classes of objects," and that's where the keyword class came from. 对象是完全相同的,除了他们在程序执行时的状态是按对象类型分组的,这就是关键字来自哪里。

Creating abstract data types (classes) is a fundamental concept in object- oriented programming.在面向对象编程中,创建抽象数据类型是基础概念。

Abstract data types work almost exactly like built-in types:抽象数据类型能够像内置类型一样精确的工作:

You can create variables of a type (called objects or instances in object-oriented parlance) and manipulate those variables (called sending messages or requests; you send a message and the object figures out what to do with it). 你可以创建一个类型的变量,这些变量在面向对象的说法里面成为对象或者实例。并且操作这些变量,操作变量成为发送消息或者请求,你可以发送消息给一个对象,这个对象根据消息处理要做的事情。

The members (elements) of each class share some commonality: 每一个类的成员(元素)具有通用性:

Every account has a balance, every teller can accept a deposit , etc. 每一个账户有余额,每一个出纳可以接受存款,等等。

At the same time, each member has its own state: Each account has a different balance, each teller has a name. 同时,每一个成员有它们自己的状态:每一个账户有不同的余额,每一个出纳有自己的名字。

Thus, the tellers, customers, accounts, transactions, etc., can each be represented with a unique entity in the computer program. 因此,出纳,客户,账户,交易等等,都能以唯一的实体在程序中展现。

This entity is the object, and each object belongs to a particular class that defines its characteristics and behaviors. 这个实体是一个对象,每一个对象属于一个类,这个类定义它的特征和行为。

So, although what we really do in object-oriented programming is create new data types, virtually all object-oriented programming languages use the "class" keyword. 所以,在面向对象编程中我们真正做的是创建新的数据类型,事实上所有的面向对象程序设计语言都使用“class”关键字。

When you see the word "type" think "class" and vice versa. 当你把“type”看做"class"的时候,反之亦然。

Since a class describes a set of objects that have identical characteristics (data elements) and behaviors (functionality), a class is really a data type because a floating point number, for example, also has a set of characteristics and behaviors. 由于一个类描述一系列具有相同特征(数据元素)和行为(方法)的对象,所以一个类就是一个数据类型,例如一个浮点型数据也有一系列的特征和行为。

The difference is that a programmer defines a class to fit a problem rather than being forced to use an existing data type that was designed to represent a unit of storage in a machine. 不同之处在于程序员根据问题定义一个类,而不是强制使用存在的数据类型,这种设计用于展示机器中的存储仓库。

You extend the programming language by adding new data types specific to your needs. 你根据具体需求来添加新的数据类型是编程语言获得延伸。

The programming system welcomes the new classes and gives them all the care and type checking that it gives to built-in types. 编程系统欣然接受新的类,并且像对待内置类型一样地管照它们和进行类型检查。

The object-oriented approach is not limited to building simulations. 面向对象方法并不是仅局限于构建仿真程序。

Whether or not you agree that any program is simulation of the system you're designing, the use of OOP techniques can easily reduce a large set of problems to a simple solution. 无论你是否赞成一下观点,即任何程序都是你所设计的系统的一种仿真,面向对象技术的应用确实可以将大量的问题很容易的降解为一个简单的解决方案。

Once a class is established, you can make as many object of that class as you like, and then manipulate those objects as if they are the elements that exist in the problem you are trying to solve. 一旦类被建立,你可以随心所欲地创建这个类的任意个对象,然后去操作这些对象,就像它们是存在于你的待求解问题中的元素一样。

Indeed, one of the challenges of object-oriented programming is to create a one-to-one mapping between the elements in the problem space and objects in the solution space. 事实上,面向对象程序设计的挑战之一,就是在问题空间的元素和解空间的对象之间创建一对一的映射。

But how do you get an object to do useful work for you? 但是,怎样才能获得有用的对象呢?

There needs to be a way to make a request of the object so that it will do something, such as complete a transaction, draw something on the screen, or turn on a switch. 必须有某种方式产生对对象的请求,使对象完成各种任务,如完成一笔交易、在屏幕上画图、打开开关等等。

And each object can satisfy only certain requests. 每个对象都只能满足某些请求。

The requests you can make of an object are defined by its interface, and the type is what determines the interface. 这些请求由对象的接口(interface)所定义,决定接口的便是类型。

A simple might be a representation of a light bulb: 以电灯泡为例来做一个简单的比喻

Light lt = new Light();
lt.on();



The interface determines the requests that you can make for a particular object. 接口确定了对某一特定对象所能发出的请求。

However, there must be code somewhere to satisfy that request. 但是,在程序中必须有满足这些请求的代码。

This, along with the hidden data, comprises the implementation. 这些代码与隐藏的数据一起构成了实现。

From a procedural programming standpoint, it's not that complicated. 从过程型编程的观点来看,这并不太复杂。

A type has a method associated with each possible request, and when you make a particular request to an object, that method is called. 在类型中,每一个可能的请求都有一个方法与之相关联,当向对象发送请求时,与之相关联的方法就会被调用。

This process is usually summarized by saying that you "send a message" (make a request) to an object, and the object figures out what to do with that message(it executes code). 此过程通常被概括为:向某个对象“发送消息”(产生请求),这个对象便知道此消息的目的,然后执行对应的程序代码。

Here, the name of the type/class si Light, the name of this particular Light object is lt, and the requests that you can make of a Light object are to turn it on, turn it off, make it brighter, or make it dimmer. 上例中,类型/类的名称是Light,特定的Light对象的名称是lt,可以向Light对象发出的请求是:打开它、关闭它、将它挑梁、将它调暗。

You create a Light object by defining a "reference" (lt) for that object and calling new to request a new object of that type. 你以下列方式建了一个Ligth对象:定义这个对象的“引用”(lt),然后调用new方法来创建该类型的新对象。

To send a message to the object, you sate the name of the object and connect it to the message request with a period (dot). 为了向对象发送消息,需要声明对象的名称,并以圆点符号连接一个消息请求。

From the standpoint of the user of a predefined class, that's pretty much all there is to programming with objects. 从预定义类的用户观点来看,这些差不多就是用对象来进行设计的全部。

The preceding diagram follows the format of the Unified Modeling Language(UML). 前面的图是UML(Unified Modelling Language, 统一建模语言)形式的图。

Each class is represented by a box, with the type name in the top portion of the box, any data members that you care to describe in the middle portion of the box, and the methods(the functions that belong to this object, which receive any messages you send
to that object) in the bottom portion of the box. 每个类都用一个方框表示,类名在方框的顶部,你所关心的任何数据成员都描述在方框的中间部分,方法(隶属于此对象的、用来接收你发给此对象的消息的函数)在方框的底部。

Often, only the name of the class and the public methods are shown in UML design diagrams, so the middle portion is not shown, as in htis case. 通常,只用类名和公共方法被示于UML设计图中,因此,方框的中部就像本例一样并未给出。

If you're interested only in the class name, then the bottom portion doesn't need to be shown, either. 如果只对类型感兴趣,那么方框的底部甚至也不需要给出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: