package yu.zhang;
public abstract class Shape {
public abstract double perimeter();
public abstract double area();
}
package yu.zhang;
public class Rectangle extends Shape{
public double i = 3; //定义矩形的长
public double j = 2; //定义矩形的宽
public double perimeter(){
return(i+i+j+j);
}
public double area(){
return(i*j);
}
}
package yu.zhang;
public class Circle extends Shape{
private double r = 4; //定义圆的半径
public double perimeter(){
return(3.14*r*2);
}
public double area(){
return(3.14*r*r);
}
}
package yu.zhang;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape rec = new Rectangle();
Shape cir = new Circle();
double recPerimeter = rec.perimeter(); //求矩形周长
double recArea = rec.area(); //求矩形面积
System.out.println("矩形的周长是:" + recPerimeter);
System.out.println("矩形的面积是:" + recArea);
double cirPerimeter = cir.perimeter(); //求圆形的周长
double cirArea = cir.area(); //求圆形的面积
System.out.println("圆形的周长是:" + cirPerimeter);
System.out.println("圆形的面积是:" + cirArea);
}
}