官术网_书友最值得收藏!

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.

對象是一個獨立的實體,它具有自己的屬性(即數據)和方法(即操作),這些屬性和方法封裝在一個可以被重復使用的結構中。

主站蜘蛛池模板: 莱阳市| 兴化市| 梁平县| 余庆县| 文昌市| 南昌市| 景宁| 大悟县| 黄石市| 泗洪县| 宣威市| 壶关县| 扬中市| 商南县| 扶绥县| 昔阳县| 新兴县| 同江市| 玉林市| 五河县| 罗江县| 峨边| 广宗县| 马山县| 彭阳县| 响水县| 九龙坡区| 福州市| 内江市| 长武县| 金山区| 永修县| 长治市| 新竹市| 贵南县| 乌兰县| 楚雄市| 溆浦县| 祁门县| 永安市| 磐安县|