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

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.

主站蜘蛛池模板: 容城县| 定兴县| 雷波县| 灵璧县| 措美县| 萝北县| 仙桃市| 东城区| 漳州市| 平凉市| 同江市| 东乡县| 如东县| 安阳县| 钟山县| 天门市| 丰城市| 绍兴市| 滕州市| 镇赉县| 涿州市| 巴里| 遂川县| 周至县| 远安县| 济源市| 饶河县| 平阳县| 东丽区| 康马县| 林甸县| 英德市| 新巴尔虎左旗| 大英县| 丰镇市| 湖南省| 白银市| 平阴县| 聂拉木县| 郎溪县| 天水市|