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

  • The Java Workshop
  • David Cuartielles Andreas G?ransson Eric Foster Johnson
  • 688字
  • 2021-06-11 13:05:21

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.

主站蜘蛛池模板: 巴马| 息烽县| 南投县| 监利县| 涿鹿县| 监利县| 彩票| 沁阳市| 资阳市| 屯留县| 金乡县| 景洪市| 威宁| 临武县| 屏东县| 汾西县| 东丰县| 揭东县| 神木县| 开平市| 西充县| 璧山县| 阳新县| 东丰县| 秀山| 满洲里市| 铁岭县| 呼图壁县| 阿拉善盟| 望都县| 元阳县| 宁陵县| 朝阳市| 龙陵县| 临海市| 通州区| 沙坪坝区| 来凤县| 浮山县| 华安县| 正安县|