1.超類和子類的設計;2.建立繼承層次;3.覆蓋方法。 程式StringLister:使用數組列表和特殊的for迴圈將一系列字元串按字母順序顯示到屏幕上。這些字元串來自一個數組和命令行參數 1 package com.jsample; 2 3 import java.util.*; 4 5 publ ...
1.超類和子類的設計;
2.建立繼承層次;
3.覆蓋方法。
程式StringLister:使用數組列表和特殊的for迴圈將一系列字元串按字母順序顯示到屏幕上。這些字元串來自一個數組和命令行參數
1 package com.jsample; 2 3 import java.util.*; 4 5 public class StringLister { 6 String[] names = { "Spanky", "Buckwheat", "Daria", 7 "Stymie", "Marianne", "Scotty", "Tommy", "Chubby" }; 8 9 public StringLister(String[] moreNames){ 10 Vector<String> list = new Vector<String>(); 11 for (int i = 0; i < moreNames.length; i++){ 12 list.add(moreNames[i]); 13 } 14 Collections.sort(list); 15 for (String name : list){ 16 System.out.println(name); 17 } 18 } 19 20 public static void main(String[] args){ 21 StringLister lister = new StringLister(args); 22 } 23 }View Code
輸出:
Buckwheat
Chubby
Daria
Marianne
Scotty
Spanky
Stymie
Tommy
程式PointTester:使用Point,Point3D,Point4D對象的程式,併在屏幕上移動它們。
1 package com.jsample; 2 3 import java.awt.*; 4 5 public class PointTester { 6 public static void main(String[] args){ 7 Point objcet1 = new Point(11,22); 8 Point3D object2 = new Point3D(7,6,64); 9 Point4D object3 = new Point4D(12,56,73,90); 10 11 System.out.println("The 2D point is located at (" + objcet1.x 12 + ", " + objcet1.y + ")"); 13 System.out.println("\tIt's being moved to (4,13)"); 14 objcet1.move(4,13); 15 System.out.println("The 2D point is now at (" + objcet1.x 16 + ", " + objcet1.y + ")"); 17 System.out.println("\tIt's being moved -10 units on the x " 18 + "and y axes"); 19 objcet1.translate(-10,-10); 20 System.out.println("The 2D point ends up at (" + objcet1.x 21 + ", " + objcet1.y + ")\n"); 22 23 System.out.println("The 3D point is located at (" + object2.x 24 + ", " + object2.y + ", " + object2.z + ")"); 25 System.out.println("\tIt's being moved to (10, 22, 71)"); 26 object2.move(10,22,71); 27 System.out.println("The 3D point is now at (" + object2.x 28 + ", " + object2.y + ", " + object2.z +")"); 29 System.out.println("\tIt's being moved -20 units on the x,y " 30 + "and z axes"); 31 object2.move(-20,-20,-20); 32 System.out.println("The 3D point ends up at (" + object2.x 33 + ", " + object2.y + ", " + object2.z + ")\n"); 34 35 System.out.println("The 4D point is located at (" + object3.x 36 + ", " + object3.y + ", " + object3.z + ", " + object3.t + ")"); 37 System.out.println("\tIt's being moved to (9, 1, 7, 4)"); 38 object3.move(9,1,7,4); 39 System.out.println("The 4D point is now at (" + object3.x 40 + ", " + object3.y + ", " + object3.z + ", " + object3.t + ")"); 41 System.out.println("\tIt's being moved 20 units on the x,y " 42 + "and z axes"); 43 object3.move(20,20,20,20); 44 System.out.println("The 4D point ends up at (" + object3.x 45 + ", " + object3.y + ", " + object3.z + ", " + object3.t + ")"); 46 } 47 }View Code
下屬class:
Point——java.awt.*中自帶。
Point3D:記錄對象三維坐標,將對象移到新坐標處,將三個坐標各移動特定距離。
1 package com.jsample; 2 3 import java.awt.*; 4 5 public class Point3D extends Point{ 6 public int z; 7 8 public Point3D(int x, int y, int z){ 9 super(x,y); 10 this.z=z; 11 } 12 13 public void move(int x, int y, int z){ 14 this.z += z; 15 super.translate(x,y); 16 } 17 }View Code
Point4D:記錄對象四維坐標,將對象移到新坐標處,將四個坐標各移動特定距離。
1 package com.jsample; 2 3 public class Point4D extends Point3D{ 4 public int t; 5 6 public Point4D(int x, int y, int z, int t){ 7 super(x,y,z); 8 if (t < 0) 9 return; 10 this.t = t; 11 } 12 13 public void move(int x, int y, int z, int t){ 14 this.z += z; 15 this.t += t; 16 super.translate(x,y); 17 } 18 }View Code
輸出:
The 2D point is located at (11, 22)
It's being moved to (4,13)
The 2D point is now at (4, 13)
It's being moved -10 units on the x and y axes
The 2D point ends up at (-6, 3)
The 3D point is located at (7, 6, 64)
It's being moved to (10, 22, 71)
The 3D point is now at (17, 28, 135)
It's being moved -20 units on the x,y and z axes
The 3D point ends up at (-3, 8, 115)
The 4D point is located at (12, 56, 73, 90)
It's being moved to (9, 1, 7, 4)
The 4D point is now at (21, 57, 80, 94)
It's being moved 20 units on the x,y and z axes
The 4D point ends up at (41, 77, 100, 114)