日期:2014-05-20 浏览次数:20715 次
package study.javase.test; abstract class Shape { public final double PI = 3.14; public Shape() { System.out.println("A shape was create."); } void draw() { System.out.println("Draw a shape"); } // abstract method abstract double area(); } // class Circle extends Shape { int xpos; int ypos; int radius; public Circle() { super(); } public Circle(int x, int y, int r) { super(); xpos = x; ypos = y; radius = r; } public void draw() { System.out.println("draw a circle"); } public double area() { return PI*radius*radius; } } // class Rectangle extends Shape { int left; int top; int width; int height; public Rectangle() { super(); } public Rectangle(int l, int t, int w, int h) { super(); left = l; top = t; width = w; height = h; } public double area() { return width*height; } } public class Abstract { public static void main(String args[]) { Shape rec = new Rectangle(0, 0, 40, 30); System.out.println(rec.area()); rec.draw(); Shape sha = new Circle(120, 130, 50); System.out.println(sha.area()); sha.draw(); } }