package Demo;
public abstract class Shape {
public abstract double getArea();
}
package Demo;
public class Square extends Shape{
private double height=0;
public Square(double height){
this.height=height;
}
public double getArea() {
return (this.height * this.height);
}
}
package Demo;
public class Circle extends Shape{
private double r=0;
private final static double PI=3.14;
public Circle(double r){
this.r=r;
}
public double getArea() {
return (PI * r * r);
}
}
package Demo;
public class TestShape {
public static void main(String[] args) {
Shape square=new Square(3);
Shape circle = new Circle(2);
System.out.println(square.getArea());
System.out.println(circle.getArea());
// Square sq= (Square) circle;
// System.out.println(sq.getArea());
}
}
运行结果
9.0
12.56
进程已结束,退出代码为 0
package Demo;
public abstract class Animal {
private int age = 0;//年龄
/**
* 不带参数的构造方法。
*/
public Animal() {
}
/**
* 带参数的构造方法 。
*/