课后练习题
//定义一个抽象父类
public abstract class Shape {
public double C; //定义周长
public double S; //定义面积
public double a = 10; //定义矩形的长
public double b = 5; //定义矩形的宽
public double r = 5; //定义圆形的半径
public double PI = 3.14; //定义圆周率
public abstract void Perimeter(); //定义一个求周长的抽象方法
public abstract void Area(); //定义一个求面积的抽象方法
}
-------------------------------------------------------------------------------------------------------------------------------------
//定义一个矩形子类
public class Rectangle extends Shape{
public void Perimeter(){
C = 2 * (a + b); //求矩形的周长
System.out.println("矩形的周长为:"+C);
}
public void Area(){
S = a * b; //求矩形的面积
System.out.println("矩形的面积为:"+S);
}
}
---------------------------------------------------------------------------------------------------------------------------------------
//定义一个圆形子类
public class Circle extends Shape{
public void Perimeter(){
C = 2 * PI * r; //求圆形的周长
System.out.println("圆形的周长为:"+C);
}
public void Area(){
S = PI * (r * r); //求圆形的面积
System.out.println("圆形的面积为:"+S);
}
}
---------------------------------------------------------------------------------------------------------------------------------------
//运行
public static void main(String[] args){
//对象的引用
Shape rectangle = new Rectangle();
Shape circle = new Circle();
rectangle.Perimeter();
rectangle.Area();
circle.Perimeter();
circle.Area();
}