正在加载图片...
E.1 Basics 7 public class Third public static void main(String args[]){ /create 2 points and initialize them Point ptA new Point(5,10); Point ptB new Point (-15,-25); /output their values ptA.printIt(); ptB.printIt () /now exchange values for first point ptA.swap(); ptA.printIt(); /ptc is a reference to ptB Point ptC ptB; /exchange the values for ptC ptC.swap(); /output the values ptB.printIt(); ptC.printIt () } Figure E.5 Objects as references. E.5 shows two objects of class Point:ptA and ptB.These objects are unique; each has its own state.However,the statement Point ptc ptB; assigns ptC a reference to object ptB.In essence,both ptB and ptC now refer to the same object,as shown in Figure E.6.The statement ptc.swap(); alters the value of this object,calling the swap()method,which exchanges the values of xCoord and yCoord.Calling the printIt()method for ptB and ptc illustrates the change in the state for the object,showing that now the x coordinate is-25 and the y coordinate is-15. Whenever objects are passed as parameters to methods,they are passed by reference.Thus,the local parameter is a reference to the argument being passed.If the method alters the state of the local parameter,it also changes the state of the argument that it was passed.In Figure E.7,the object ptD is passed to the method change(),where the swap()method is called.When they return from change(),the values of the x and y parameters for ptD are exchanged. The Java keyword this provides an object a reference to itself.The this reference is useful when an object needs to pass a reference of itself to another object.E.1 Basics 7 public class Third { public static void main(String args[]) { // create 2 points and initialize them Point ptA = new Point(5,10); Point ptB = new Point(-15,-25); // output their values ptA.printIt(); ptB.printIt(); // now exchange values for first point ptA.swap(); ptA.printIt(); // ptC is a reference to ptB Point ptC = ptB; // exchange the values for ptC ptC.swap(); // output the values ptB.printIt(); ptC.printIt(); } } Figure E.5 Objects as references. E.5 shows two objects of class Point: ptA and ptB. These objects are unique; each has its own state. However, the statement Point ptC = ptB; assigns ptC a reference to object ptB. In essence, both ptB and ptC now refer to the same object, as shown in Figure E.6. The statement ptC.swap(); alters the value of this object, calling the swap() method, which exchanges the values of xCoord and yCoord. Calling the printIt() method for ptB and ptC illustrates the change in the state for the object, showing that now the x coordinate is −25 and the y coordinate is −15. Whenever objects are passed as parameters to methods, they are passed by reference. Thus, the local parameter is a reference to the argument being passed. If the method alters the state of the local parameter, it also changes the state of the argument that it was passed. In Figure E.7, the object ptD is passed to the method change(), where the swap() method is called. When they return from change(), the values of the x and y parameters for ptD are exchanged. The Java keyword this provides an object a reference to itself. The this reference is useful when an object needs to pass a reference of itself to another object
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有