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

Working with Objects in Java

Objects are to classes what variables are to data types. While classes define the structure and possible actions of a certain data type, objects are actual usable parts of the computer memory containing that data. The action of creating an object is known as making an instance of a class. In a sense, it is like making a copy of the template and then modifying it by accessing its variables or methods. Let's see this in action:

Computer myPC = new Computer( 2.5 );

myPC is the actual object. We would say that myPC is an object of the class Computer in colloquial terms.

The different fields and methods inside the class can be accessed by typing the name of the object followed by a period and the name of the variable or method you want to address. Making any changes to the variables or calling the methods will take effect only within the scope of that object. If you had more objects of the same class in your program, each one of them would have a piece of memory of its own. An example of how to address a method is as follows:

myPC.setCpuSpeed( 2.5 );

An example of how to address a variable, on the other hand, would be the following assignment:

myPC.cpuSpeed = 2.5;

Because of the way the Computer class has been defined, the last two code listings have the exact same effect. The whole class has been defined—by default—as public, which means that all the methods, variables, and objects from the class are available to be called with the mechanism described previously. It could be necessary to prevent users from directly interacting with the variables within the class and only allow their modification through certain methods.

The different components within a class can be defined as public or private. The former will make the component available to be used as shown so far, while the latter will hinder the ability of other developers to access that part. The following example shows how to make the cpuSpeed variable private:

Example02.java

1  class Computer {

2      // variables

3      private double cpuSpeed;  // in GHz

4  

5      // constructor

6      Computer() {

7          cpuSpeed = 0;

8      }

9  

10     // methods

11     void setCpuSpeed ( double _cpuSpeed ) {

12         cpuSpeed = _cpuSpeed;

13     }

14 

15     double getCpuSpeed () {

16         return cpuSpeed;

17     }

18 }

The result of this code listing is the same as before:

2.5

Process finished with exit code 0

If you tried to access the cpuSpeed variable directly from the Example02 class, the program would throw an exception. The following example shows such a case. Try it out to see how the debugger informs you when you try to access a private variable:

Example03.java

20 public class Example03 {

21     public static void main(String[] args) {

22         Computer myPC = new Computer();

23         myPC.setCpuSpeed( 2.5 );

24         System.out.println( myPC.cpuSpeed );

25     }

26 }

The result of this program is:

Example03.java:23: error: cpuSpeed has private access in Computer

        System.out.println( myPC.cpuSpeed );

1 error

Process finished with exit code 1.

What the compiler shows is an error in the Computer class, which has been derived from java.lang.

主站蜘蛛池模板: 金门县| 吉木萨尔县| 天峻县| 吴江市| 平邑县| 广州市| 通榆县| 土默特右旗| 南靖县| 齐齐哈尔市| 霍林郭勒市| 抚远县| 竹山县| 松滋市| 鞍山市| 大连市| 绿春县| 铜山县| 宁德市| 桐城市| 通江县| 石嘴山市| 汪清县| 漯河市| 华亭县| 阜新| 秦皇岛市| 古交市| 安福县| 扬州市| 左云县| 莆田市| 盐城市| 台北县| 宜宾市| 丰都县| 双流县| 高青县| 肃宁县| 唐山市| 静宁县|