Lập trình hướng đối tượng

Đã học:

§  biến (variables), biểu diễn dữliệu (phân biệt bởi kiểu)‏

§  phương thức (tĩnh), biểu diễn hành vi

v  đối tượng:biến chứa dữliệu và hành vi.

§  Biểu diễn dữliệu của đối tượng.

§  Biểu diễn hành vi của đối tượng.

v  Lớp:

§  Khối cơbản của một chương trình JAVA

§  Kiểu dữliệu cho đối tượng

pdf84 trang | Chia sẻ: Mr Hưng | Lượt xem: 735 | Lượt tải: 0download
Bạn đang xem trước 20 trang nội dung tài liệu Lập trình hướng đối tượng, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
+= 6; p1.z + 2; p2.x += 2; p2.y += 4; p2.z += -2; p3.x += 1; p3.y += 7; p3.z += 17; 65 Tránh rườm rà v Phương thức tĩnh. // Shifts the location of the given point. public static void translate( Point p, int dx, int dy, int dz) { p.x += dx; p.y += dy; p.z += dz; } v Example: // move p2 translate(p2, 2, 4, 6); v Question: Tại sao không trả về point vừa sửa? 66 Phương thức tĩnh có tốt ? v Client code: cần phải viết riêng phương thức tĩnh! v Cú pháp không phù hớp với khi tương tác với đối tượng : translate(p2, 2, 4); We want : p2.translate(2, 4); 67 Lớp có hành vi v Viết lớp: trộn lẫn trạng thái và hành vi. v Dịch chuyển Point liên quan đến dữ liệu x/y/z của đối tượng Point3D -> thuộc về lớp Point3D. 68 Phương thức của thực thể v instance method: Phương thức bên trong đối tượng, thao tác trên chính đối tượng này. v Cú pháp khai báo: public () { ; } v How does this differ from previous methods? 69 Phương thức thực thể v Một phương thức thực thể của đối tượng có thể tham chiếu tới các trường của đối tượng đó. public void translate(int dx, int dy, int dz) { x += dx; y += dy; z += dz; } v How does the translate method know which x, y, z to modify? 70 Tham số ngầm định v Mỗi lần gọi phương thức thực thể đều tương ứng với một đối tượng cụ thể. §  Example: p1.translate(11, 6, 2); v Code cho các phương thức này sẽ hiểu ngầm đối tượng nào. v Tham số ngầm định (implicit parameter): Đối tượng có phương thức thực thể được gọi. 71 Point3D object diagrams v  Mỗi đối tượng có một phương thức translate riêng : Point3D p1 = new Point3D(7, 2, -2); Point3D p2 = new Point3D(4, 3, 2); p1: p2: public void translate(int dx, int dy, int dz) { ... } y: x: 7 2 z: -2 public void translate(int dx, int dy, int dz) { ... } y: x: 4 3 z: 2 18 72 Tracing v What happens? p1.translate(11, 6, 3); p2.translate(1, 7, -10); p1: p2: public void translate(int dx, int dy, int dz) { ... } y: x: 7 2 z: -2 public void translate(int dx, int dy, int dz) { ... } y: x: 4 3 z: 2 8 1 5 10 -8 Point3D : Version 3 public class Point3D { public int x; public int y; public Point3D( int initialX, int initialY, int initialZ) { x = initialX; y = initialY; z = initialZ; } // Changes the location of this Point3D object. public void translate(int dx, int dy, int dz){ x += dx; y += dy; z += dz; } } 73 74 Client program: Version 3 public class PointMain3 { public static void main(String[] args) { // create two Point3D objects Point3D p1 = new Point3D(5, 2, -2); Point3D p2 = new Point3D(4, 3, 2); // print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); // move p2 and then print it again p2.translate(2, 4, -2); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); } } Output: p1 is (5, 2, -2)‏ p2 is (4, 3, 2)‏ p2 is (6, 7, 0)‏ 75 Exercises v  Viết phương thức thực thể tên distanceFromOrigin tính và trả về khoảng cách giữa đối tượng hiện tại và vị trí (0, 0, 0). v  Viết một phương thức thực thể tên distance nhận tham số vào là một đối tượng Point3D và tính khoảng cách giữa đối tương này với đối tượng hiện tại. v  Viết một phương thức thực thể tên setLocation nhận 3 tham số x, y, và z và thay đổi vị trí của đối tượng hiện tại theo 3 vị trí mới này. v  Sửa client code để sử dụng phương thức mới. 76 Solutions public class Point3D { public int x; public int y; public int z; // Changes the location of this Point3D object. public void translate(int dx, int dy, int dz) { setLocation(x + dx, y + dy, z + dz); } // Returns the distance from this Point object to the origin public double distanceFromOrigin() { return Math.sqrt(x * x + y * y + z * z); } // Returns the distance from this Point3D object to the given point public double distance(Point3D other) { int dx = x - other.x; int dy = y - other.y; int dz = z – other.z; return Math.sqrt(dx * dx + dy * dy + dz * dz); } // Sets this Point3D object's location public void setLocation(int newX, int newY, int newZ) { x = newX; y = newY; z = newZ; } } Exercise v Recall the client program from the previous exercise that produced this output: p1 is (7, 2, -5)‏ p1's distance from origin = 8.831760866327846 p2 is (4, 3, 2)‏ p2's distance from origin = 5.385164807134504 p1 is (18, 8, -2)‏ p2 is (5, 10, 20)‏ v Modify the program to use our new instance methods. v Also add the following output to the program: distance from p1 to p2 = 13.152946437965905 77 78 Solution public class PointProgram { public static void main(String[] args) { // create two Point objects Point p1 = new Point(); p1.setLocation(7, 2); Point p2 = new Point(); p2.setLocation(4, 3); // print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p1's distance from origin = " + p1.distanceFromOrigin()); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); System.out.println("p2's distance from origin = " + p2.distanceFromOrigin()); // move points and then print again p1.translate(11, 6); System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); p2.translate(1, 7); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); System.out.println("distance from p1 to p2 = " + p1.distance(p2)); } } Solution public class PointProgram { public static void main(String[] args) { // create two Point3D objects Point3D p1 = new Point3D(7, 2, -5); Point3D p2 = new Point3D(4, 3, 2); // print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + “, “ + p1.z + ")"); System.out.println(“p1’s distance from origin = “ + p1.distanceFromOrigin()); System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")"); System.out.println("p2's distance from origin = " + p2.distanceFromOrigin()); // move points and then print again p1.translate(11, 6, 3); System.out.println("p1 is (" + p1.x + ", " + p1.y + “, “ + p1.z + ")"); p2.translate(1, 7, 22); System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")"); System.out.println("distance from p1 to p2 = " + p1.distance(p2)); } } 79 80 toString or not toString 81 Latest version of client code public class PointMain3 { public static void main(String[] args) { // create two Point objects Point p1 = new Point(7, 2, -5); Point p2 = new Point(4, 3, 2); // print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); // move p2 and then print it again p2.translate(2, 4, -2); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); } } v Any remaining redundancies? 82 Printing points v Instead of System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); v It would be nice to have something more like: System.out.println("p1 is " + p1); v What does this line currently do? Does it even compile? It will print: p1 is Point@9e8c34 83 toString v When an object is printed or concatenated with a String, Java calls the object's toString method. System.out.println("p1 is " + p1); is equivalent to: System.out.println("p1 is " + p1.toString()); v Note: Every class has a toString method. 84 toString v The default toString behavior is to return the class's name followed by gibberish (as far as you are concerned). v You can replace the default behavior by defining a toString method in your class. 85 toString method syntax v The toString method, general syntax: public String toString() { ; } v NB: The method must have this exact name and signature (i.e., public String toString()). v Example: // Returns a String representing this Point. public String toString() { return "(" + x + ", " + y + “, “ + z + ")"; }

Các file đính kèm theo tài liệu này:

  • pdf01_object_2249.pdf
Tài liệu liên quan