Object-oriented programming (OOP) refers to a special type of programming that combines data structures with functions to create re-usable objects.
面向对象的程序设计涉及一种专用类型的程序设计,该技术将数据结构与函数相结合产生可重用的对象。
Otherwise, the term object-oriented is generally used to describe a system that deals primarily with different types of objects, and where you can take the actions depends on what type of object you are manipulating. For example, an object-oriented draw program might enable you to draw many types of objects, such as circles, rectangles, triangles, etc. Applying the same action to each of these objects, however, would produce different results. If the action is Make 3D, for instance, the result would be a sphere, box, and pyramid, respectively.
另一方面,术语“面向对象”通常用来描述一种系统,这种系统主要处理不同类型的对象,在这儿人们所采取的行为取决于他们正处理的对象的类型。例如,一种面向对象的绘图程序能使人们绘制多种类型的对象,如圆、长方形和三角形等。但是将同样的行为应用于不同的对象将产生不同的结果。例如,如果这一行为是制作三维,结果将分别形成球体、长方体和棱锥。
Many languages support object oriented programming. In OOP data and functions are grouped together in objects (encapsulation). An object is a particular instance of a class. Each object can contain different data, but all objects belonging to a class have the same functions (called methods). So you could have a program with many e-mail objects, containing different messages, but they would all have the same functionality, fixed by the email class. Objects often restrict access to the data (data hiding).
许多语言都支持面向对象的程序设计。在面向对象的程序设计中,数据和功能组合在一起归入对象内(封装)。一个对象是一个类的特例。每一个对象都可以包含不同的数据,但属于同一个类的所有对象具有相同的功能(称为方法)。于是,人们可以有一个带有许多电子邮件对象的程序,这些电子邮件对象包含有不同的信息,但具有电子邮件类固有的相同的功能。对象经常限制对数据的存储(数据隐藏)。
Classes are a lot like types-the exact relationship between types and classes can be complicated and varies from language to language.
类很像类型——它们之间的关系会很复杂,而且随着语言的变化而变化。
Via inheritance, hierarchies of objects can share and modify particular functions. You may have code in one class that describes the features all e-mails have (a sender and a date, for example) and then, in a sub-class for email containing pictures, add functions that display images. Often in the program you will refer to an e-mail object as if it was the parent (super-class) because it will not matter whether the e-mail contains a picture, or sound, or just text. This code will not need to be altered when you add another sub-class of e-mail objects, containing (say) electronic cash.
通过继承,不同级别的对象可以共享和修改特殊的功能。可以在一个类中有代码描述所有邮件具有的特点(例如:发送者和日期期),而在包含图片的子类中,增加显示图像的功能。在程序中人们经常查阅一个邮件对象,把它作为一个父类,闵为该邮件是杏包含图片、声音或仅仅一种文木无关紧要。当你添加另一个邮件对象的子类,譬如包含电子现金的时候不需要改变代码。
Sometimes you may want an action on a super-class to produce a result that depends on what sub-class it "really is". For example, you may want to display a list of email objects and want each sub-class (text, image, etc) to display in a different colour. In many languages it is possible for the super-class to have functions that sub-classes change to suit their own purposes (polymorphism, implemented by the compiler using a technique called dynamic binding). So each email sub-class may supply an alternative to the default, printing function, with its own colour.
有时候人们可能希望在父类上的一个行为可以根据子类的“真实”内容产生结果。例如:你可能想显示邮件对象的列表并且希望每个子类(文本、图像等)显示出不同的颜色。 很多语言都可能具有多态性,即父类具有的功能使子类变化以适成它们自己的目的(多态性,由编译器使用动态绑定技术来实现)。这样,每个邮件子类可以提供默认打卬功能的选择,用自己的颜色显示。
In many languages it is possible to find out what class an object is (run time type information) and even what functions are connected with it (introspection / reflection). Others, like C++ have little run time information available (at least in the standard language-individual libraries of objects can support RTTI with their own conventions).
在很多面向对象的讲言中,可以找出一个对象属于哪个类(运行时信息),甚至相关的方法(内省)。其他的像C++则没有可用的运行时信息(至少在标准语言,对象的个别 的库以它们自己的约定支持运行时信息。
There are at least three approaches to OO languages: Methods in Classes, Multi-Methods Separate from Classes, Prototypes.
至少有三种方法处理面向对象的语言:类的方法、与类分离的多方法和原型。
Methods in Classes
Many languages follow Smalltalk in associating functions (methods) with classes. The methods form part of a class definition and the language implementation will have (this is a low-level detail hidden from the programmer) a vtable for each class which links methods to their implementations. This indirection is necessary to allow polymorphism, but introduces a performance penalty. In some languages (C++, at least), only some methods, marked as virtual by the programmer, are treated in this way.
Smalltalk以及其后的许多语言部将方法与类关联。方法形成类定义的一部分。并且与语言实现每个类有一个虚函数表将方法与执行联系起来。为了允许多态性,这种间接是必须的,但引起了性能下降。在某些语言中(至少C++),只有程序员标记为虚拟的部分方法用这种方式来处理。
Multi-Methods Separate from Class
Some languages (e.g. common Lisp / CLOS) allow functions to specialise on the class of any variable that they are passed (multi-methods). Functions cannot be associated with one class because different versions of the function may exist for many different combinations of classes.
某些语言(例如Lisp/CLOS).允许函数专门研究不同变量的类以进行函数传递(多方法),多个函数不能和一个类相关联,因为类的不同结合可能存在不同的函数版本。
Prototypes
Other OO languages do away with classes completely (e.g. Self). Prototype-based languages create new objects using an existing object as an example (prototype). Apart from solving some problems with dynamic object creation, this approach also encourages delegation (function calls are passed to other objects) rather than inheritance.
其他面向对象的语言完全取消了类(例如Self)。基于原型的语言将已有的对象作为范例(原型)生成新的对象。除了解决一些动态对象产生的问题之外,这种方法还鼓励授权(函数调用被传递到其他对象)而不是继承。