- 雙語(yǔ)版Java程序設(shè)計(jì)
- 何月順主編
- 1765字
- 2018-12-27 20:14:09
2.1 Concepts of OOP
As you know, all computer programs include two elements: code and data. Conceptually, a program can be organized based on code and data. That is, some programs are written around “what is happening” and others are written around “who is being affected”. These are the two examples that show how a program is constructed. The first way is called the process-oriented model(面向過(guò)程模式).
所有程序一般包括兩個(gè)元素:代碼和數(shù)據(jù)。其中,一些程序側(cè)重處理的過(guò)程,一些程序側(cè)重處理的對(duì)象。
This approach characterizes a program as a series of linear steps (that is, code). The process-oriented model can be viewed of as series of linear steps (that is, code). In the process-oriented model code is acted on data. C is paradigm for using this model. However problems appear as program grows larger and more complex.
To manage increasing complexity(復(fù)雜性), the second approach was invented. We called it called object-oriented programming(面向?qū)ο缶幊?. This model is described as data controlling access to code. As you will know, by switching the controlling entity to data, you can achieve several organization benefits.
面向?qū)ο缶幊棠軌蚬芾肀容^復(fù)雜的項(xiàng)目,這種編程模型使用數(shù)據(jù)來(lái)控制訪問(wèn)代碼。
The following problem is that how to obtain object. The same types of entity have the same properties. So we can extract those same features from real-entities. This approach is called abstraction.
Abstraction: Abstraction is the presentation of simple concept (or object) to the external world. Abstraction concept is to describe a set of similar concrete entities. It focuses on the essential, inherent aspects of an entity and ignoring its accidental properties. The concept is usually used during analysis: deciding only with application-domain concepts, not making design and implementation decisions.
抽象是對(duì)外部世界簡(jiǎn)單概念(對(duì)象)的表達(dá),是對(duì)同類實(shí)體集合的描述。它側(cè)重于實(shí)體的本質(zhì)、內(nèi)在的方面,而忽略其偶然性。抽象概念通常在分析階段使用,在設(shè)計(jì)階段和實(shí)施階段不使用。
Two popular abstractions: procedure abstraction and data abstraction. Procedure abstraction is to decompose problem to many simple sub-works. Data abstraction is to collect essential elements composing to a compound data. These two abstractions have the same objective: reusable and replaceable. Figure 2.1 shows an example of data abstraction to abstract doors as a data structure with essential properties.

Figure 2.1 Abstract of Door
Hierarchical classification is used to manage abstraction. This allows you to layer the semantics of complex systems, breaking them into more manageable pieces. From the outside, the car is a single object. Once inside, you see that the car consists of several subsystems: steering, brakes, sound system, seat belts, heating, cellular phone, and so on. In turn, each of these subsystems is made up of more specialized units. For instance, the sound system consists of a radio, a CD player, and /or tape player. The point is that you manage the complexity of the car (or any other complex system) through the use of hierarchical abstraction.
層次化分類用來(lái)管理抽象,允許將一個(gè)復(fù)雜的系統(tǒng)分層,將一個(gè)系統(tǒng)分解成一些可以管理的部分。
2.1.1 Class
A class is a construct that is used as a blueprint to create instances of itself. For example: There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class also is the blueprint from which individual objects are created.
類是一種被用來(lái)創(chuàng)建實(shí)例的模板。
2.1.2 Object
In everyday life, an object is anything that is identifiably a single material item. An object can be a car, a house, a book, a document, or a paycheck. For our purposes, we’re going to extend that concept a bit and think of an object as anything that is a single item that you might want to represent in a program. We’ll therefore also include living “objects”, such as a person, an employee, or a customer, as well as more abstract “objects”, such as a company, a database, or a country.
任何事物都可以看作對(duì)象,對(duì)象可以代表整個(gè)程序,也可以包括一些有生命的“對(duì)象”,如一個(gè)人,雇員,顧客,還包括一些比較抽象的“對(duì)象”,如一個(gè)企業(yè),一個(gè)數(shù)據(jù)庫(kù)或一個(gè)國(guó)家。
2.1.3 Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.
Encapsulation allows an object to separate its interface from its implementation. The data and the implementation code for the object are hidden behind its interface. So encapsulation hides internal implementation details from users. The power of encapsulated code is that everyone knows how to access it and thus can use it regardless of the implementation details and without fear of unexpected side effects.
封裝將對(duì)象的接口與實(shí)現(xiàn)分開(kāi),實(shí)現(xiàn)對(duì)數(shù)據(jù)和代碼的隱藏,提高數(shù)據(jù)的安全性。
In Java the basis of encapsulation is the class. A class defines the structure and behavior (data and code) that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class, as if it were stamped out by a mold in the shape of the class. For this reason, objects are sometimes referred to as instance of class. Thus, a class is a logical construct; an object has physical reality.
When you create a class, you will specify the code and data that constitute that class. Collectively, these elements are called members of the class. Specifically the code that operates on that data is referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods. (If you are familiar with C/C++, it may help to know that what a Java programmer calls a method a C/C++ programmer calls a function.) In properly written Java programs, the methods define how the member variables can be used. This means that the behavior and interface of a class are defined by the methods that operate its instance data.
類由代碼和數(shù)據(jù)組成,代碼和數(shù)據(jù)是類的成員。類本身體現(xiàn)了封裝特性。
2.1.4 Inheritance
In object-oriented programming (OOP), inheritance is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects. In classical inheritance where objects are defined by classes, classes can inherit other classes. The new classes, known as subclasses (or derived classes), inherit attributes and behavior (i.e. previously coded algorithms) of the pre-existing classes, which are referred to as superclasses, ancestor classes or base classes. The inheritance relationships of classes give rise to a hierarchy. In prototype-based programming, objects can be defined directly from other objects without the need to define any classes, in which case this feature is called differential inheritance.The inheritance concept was invented in 1967 for Simula.
在面向?qū)ο缶幊?OOP)中,繼承可以通過(guò)創(chuàng)造一系列屬性和行為的方式來(lái)劃分和重用代碼。這些屬性和方法依賴先前已經(jīng)創(chuàng)建的對(duì)象,從而提高代碼的重用率。
Without the use of hierarchies, each object would need to define all of its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus, it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case. Let’s take a closer look at this process.
Most people naturally view the world as made up of objects that are related to each other in hierarchical way, such as animals, mammals, and dogs. If you wanted to describe animals in an abstract way, you would say they have some attributes, such as size intelligence, and type of skeletal system. Animals also have certain behavioral aspects; they eat, breathe, and sleep. This description of attributes and behavior is the class definition for animals.
2.1.5 Polymorphism
Polymorphism (from the Greek, meaning “many forms”) is the ability to create a variable, a function, or an object that has more than one form. In Java, polymorphism is the ability of objects to react differently when presented with different information, known as parameters. In a functional programming language, it is the only way to complete two different tasks is to have two functions with different names.
多態(tài)性:發(fā)送消息給某個(gè)對(duì)象,讓該對(duì)象自行決定響應(yīng)何種行為。通過(guò)將子類對(duì)象引用賦值給超類對(duì)象引用變量,動(dòng)態(tài)實(shí)現(xiàn)方法調(diào)用。
- 我的J2EE成功之路
- 網(wǎng)上沖浪
- 自動(dòng)控制理論(非自動(dòng)化專業(yè))
- 可編程序控制器應(yīng)用實(shí)訓(xùn)(三菱機(jī)型)
- 計(jì)算機(jī)網(wǎng)絡(luò)安全
- 傳感器與新聞
- PVCBOT機(jī)器人控制技術(shù)入門(mén)
- Mastering Game Development with Unreal Engine 4(Second Edition)
- 基于Xilinx ISE的FPAG/CPLD設(shè)計(jì)與應(yīng)用
- 液壓機(jī)智能故障診斷方法集成技術(shù)
- 統(tǒng)計(jì)挖掘與機(jī)器學(xué)習(xí):大數(shù)據(jù)預(yù)測(cè)建模和分析技術(shù)(原書(shū)第3版)
- MATLAB-Simulink系統(tǒng)仿真超級(jí)學(xué)習(xí)手冊(cè)
- HBase Essentials
- 筆記本電腦使用與維護(hù)
- Red Hat Enterprise Linux 5.0服務(wù)器構(gòu)建與故障排除