标签:
1 import java.awt.*; 2 import java.util.Scanner; 3 4 import javax.swing.*; 5 6 public class Test extends JFrame { 7 public static void main(String[] args) { 8 Comparable a1 = new GeometricObject("white",10); 9 Comparable a2 = new GeometricObject("yellow",20); 10 Comparable a3 = GeometricObject.max(a1, a2); 11 12 13 System.out.println("the bigger on is:"); 14 System.out.print(((GeometricObject)a3).getColor()); 15 } 16 }
1 // GeometricObject.java: The abstract GeometricObject class 2 public class GeometricObject implements Comparable{ 3 private String color = "white"; 4 private boolean filled; 5 private int a; 6 7 /**Default construct*/ 8 protected GeometricObject() { 9 } 10 11 /**Construct a geometric object*/ 12 protected GeometricObject(String color, int a) { 13 this.color = color; 14 this.a = a; 15 } 16 17 public static Comparable max(Comparable c1,Comparable c2){ 18 if(c1.compareTo(c2)>0) 19 return c1; 20 else return c2; 21 } 22 public int compareTo(Object o){ 23 if(this.getArea() > ((GeometricObject)o).getArea()) 24 return 1; 25 else if (this.getArea() == ((GeometricObject)o).getArea()) 26 return 0; 27 else return -1; 28 } 29 /**Getter method for color*/ 30 public String getColor() { 31 return color; 32 } 33 34 /**Setter method for color*/ 35 public void setColor(String color) { 36 this.color = color; 37 } 38 39 /**Getter method for filled. Since filled is boolean, 40 so, the get method name is isFilled*/ 41 public boolean isFilled() { 42 return filled; 43 } 44 45 /**Setter method for filled*/ 46 public void setFilled(boolean filled) { 47 this.filled = filled; 48 } 49 50 /**Abstract method findArea*/ 51 public int getArea(){ 52 return a*a; 53 } 54 55 /**Abstract method getPerimeter*/ 56 public double getPerimeter(){ 57 return 1; 58 } 59 }
标签:
原文地址:http://www.cnblogs.com/wanjiang/p/5587402.html