双语版Java程序设计
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

2.2 More Details on Object-Oriented Programming

The example lets use is that of a car. Object-oriented programming thinks of real world things as objects, so in the case of the example the car is an object. Objects have two parts to them, data and operations, which can be carried out on this data. So in the car example, the data might contain the speed of the car, the height of the seat and whether the horn is currently being rung or not. There are several different operations, which can be carried out on the car, the rider may drive faster, and they might want to change the gear. So in this simple object we have the following:

作为对象应该具备两个部分:数据和操作,操作是基于数据来完成相应动作。以车为例来说明,车的数据包括车速、座位的高度、喇叭是否能响。车可有几种不同的操作。当司机加速的时候,改变车速这个数据就可以实现。

Data

● Speed

● Height of seat

● Status of the horn

Operations

● Drive Faster

● Adjust gear

● Blow horn

2.2.1 Encapsulation of Car

Encapsulation is an important part of OO programming, but it’s not difficult. In Java Encapsulation is implemented by a class(类), a class is the generic form of an object.

So in our example the class is Car while the object (an instance of the class) is, perhaps MyCar. Within the object there are so-called instance variables, in our example MyCar will have instance variables for speed, height of seat and status of the horn. So when an operation (in Java a method) operates on an object it changes the instance variables for that object. If the Faster Speed method were called for the object MyCar then the instance variable of speed, within MyCar, would be altered accordingly.

Car是类,MyCar是Car创建的对象(类的实例)。

2.2.2 Inheritance of Car

When a class is extended, to create a sub-class(子类), all of the properties (variables and methods) of the original class still exist within the new class along with others, which have been added.

The Car in the example is a very simple one; if we wanted to extend it to have gears we would simply create a new class based on Car but which had a new variable called gear and two new methods, one to change up a gear and one to change down.

当一个类创建了子类,父类的所有属性(变量和方法)仍将与新创建的子类的属性共存。

Note that Java does not directly support multiple inheritances.

注:Java不支持类的多重继承。

Figure 2.2 shows the Inheritance properties, which can constitute a Car finally.

Figure 2.2 Inheritance Tree of Car

Figure 2.3 shows the Buick car inherits the encapsulation of all its super classes.

Figure 2.3 Inheritance of Buick Car

2.2.3 Polymorphism of Car

Object-oriented languages, which include Java, allow different methods to be run depending on what type of parameters is specified. So if our car were to be involved in a collision with a object, the object would be passed as a parameter to a collision method, obviously collision with a fly will have very different affects to collision with a Bus. The fly may cause the car no damage while collision with a Bus may cause a sudden loss of speed and major damage.

This ability of a method to react to different parameters is achieved by overloading, a number of methods are written (with the same name) but each one has a different set of input parameters.

So for our example there will be two methods called collision, one, which accepts a parameter of type fly while the other, accepts a parameter of type Bus.

多态性是指同一操作对不同的类可以呈现不同的行为,如参数的个数,类型不同。重载是通过不同的参数列表实现方法的不同功能,一系列的方法可以用相同的名字来编写,但是每个方法却有不同的输入参数。

2.2.4 Conclusion on Object-Oriented Programming

Object-oriented programming is fundamentally different to traditional functional programming, used correctly it can lead to the development of very robust, easily expandable and maintainable code. Objects are everywhere around us, while the class they belong to is not always obvious. We use encapsulation all the time, when we change gear in a car we do not need to know what happens with the clutch or gearbox we simply need to know that moving the gear lever will change gear. If this were likened to an object, the gear lever would be the method while the internal workings of the clutch and gearbox would be the instance variables. Expanding this simple car to be a racing car with wings and spoilers is examples of inheritance while the car’s ability to cope with steering movements to the left and right is an example of polymorphism.

Java is an object-oriented programming language. But what are objects? An object is a self-contained entity which has its own private collection of properties (i.e. data) and methods (i.e. operations) that encapsulate functionality into a reusable and dynamically loaded structure.

对象是一个独立的实体,它具有自己的属性(即数据)和方法(即操作),这些属性和方法封装在一个可以被重复使用的结构中。