package exercise;
class Figure{
double dim1;
double dim2;
Figure(double d1, double d2){
this.dim1=d1;
this.dim2=d2;
}
public double area() {
System.out.println("父类中的方法,没有实际意义,需要在子类中重写。");
return 0;
}
}
class Rectangle extends Figure{
Rectangle(double d1, double d2) {
super(d1, d2);
}
public double area() {
System.out.println("长方形的面积="+dim1*dim2);
super.area();
return 0;
}
}
class Triangle extends Figure{
Triangle(double d1, double d2) {
super(d1, d2);
}
public double area() {
System.out.println("三角形的面积="+dim1*dim2/2);
super.area();
return 0;
}
}
public class zuoye510 {
public static void main(String[] args) {
Figure f=new Figure(4,5);
f=new Rectangle(0,0);
f.area();
f=new Triangle(0,0);
f.area();
}
}