正在加载图片...
E.1 Basics creates an object with the name ptA of type Point.Furthermore,the state of this object is initialized to x=0 and y=15.We initilize objects using a constructor. A constructor looks like an ordinary method except that it has no return value and it must have the same name as the name of the class.The constructor is called when the object is created with the new statement.In the statement Point ptA new Point(0,15);,the values 0 and 15 are passed as parameters to the constructor,where they are assigned to the data xCoord and yCoord. With the Point class written the way it is,we could not create a point using the statement Point ptA new Point () because there is no constructor that has an empty parameter list.If we wanted to allow the creation of such an object,we have to create a matching constructor. For example,we could add the constructor shown in Figure E.4 to the class Point.Doing so allow us to create an object using the statement Point ptA new Point();.In this case,the x and y coordinates for the point are initialized to 0. Having two constructors in the Point class leads to an interesting issue.In some ways,a constructor behaves like an ordinary method.Two constructors for this class essentially means two methods with the same name.This feature of object-oriented languages is known as method overloading.A class may class Point /constructor to initialize the object public Point(int i,int j){ xCoord =i; yCoord j; /exchange the values of xCoord and yCoord public void swap(){ int temp; temp xCoord; xCoord yCoord; yCoord temp; public void printIt()f System.out.println("X coordinate ="xCoord); System.out.println("Y coordinate ="yCoord); } /class data private int xCoord;//X coordinate private int yCoord;//y coordinate Figure E.3 A point.E.1 Basics 5 creates an object with the name ptA of type Point. Furthermore, the state of this object is initialized to x = 0 and y = 15. We initilize objects using a constructor. A constructor looks like an ordinary method except that it has no return value and it must have the same name as the name of the class. The constructor is called when the object is created with the new statement. In the statement Point ptA = new Point(0,15);, the values 0 and 15 are passed as parameters to the constructor, where they are assigned to the data xCoord and yCoord. With the Point class written the way it is, we could not create a point using the statement Point ptA = new Point(); because there is no constructor that has an empty parameter list. If we wanted to allow the creation of such an object, we have to create a matching constructor. For example, we could add the constructor shown in Figure E.4 to the class Point. Doing so allow us to create an object using the statement Point ptA = new Point();. In this case, the x and y coordinates for the point are initialized to 0. Having two constructors in the Point class leads to an interesting issue. In some ways, a constructor behaves like an ordinary method. Two constructors for this class essentially means two methods with the same name. This feature of object-oriented languages is known as method overloading. A class may class Point { // constructor to initialize the object public Point(int i, int j) { xCoord = i; yCoord = j; } // exchange the values of xCoord and yCoord public void swap() { int temp; temp = xCoord; xCoord = yCoord; yCoord = temp; } public void printIt() { System.out.println("X coordinate = " + xCoord); System.out.println("Y coordinate = " + yCoord); } // class data private int xCoord; // X coordinate private int yCoord; // Y coordinate } Figure E.3 A point
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有