抽象類實現Java 多型特性案例
package javarefence.ninth.introducingclasses.introducingmethods.charter870;
abstract class Figure { double dim1; double dim2; Figure(double a,double b){ dim1=a; dim2=b; } abstract double area();
}
package javarefence.ninth.introducingclasses.introducingmethods.charter870;
public class Rectangle extends Figure { Rectangle(double a,double b){ super(a,b); }
//@Override public double area() { // TODO Auto-generated method stub return dim1*dim2; }
}
package javarefence.ninth.introducingclasses.introducingmethods.charter870;
public class Triangle extends Figure { Triangle (double a,double b){ super(a,b); } public double area(){ return dim1*dim2/2; }
}
package javarefence.ninth.introducingclasses.introducingmethods.charter870;
public class AbstractArea {
public static void main(String[] args) { // TODO Auto-generated method stub // Figure f=new Figure(); Rectangle r= new Rectangle(9,6); Triangle t= new Triangle(12,16); Figure figureRefence; figureRefence=r; System.out.println(figureRefence.area()); figureRefence=t; System.out.println(figureRefence.area());
}
}