父类:
public abstract class Shape {
private double perimeter;
private double area;
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public Shape(double perimeter,double area){
this.perimeter=perimeter;
this.area=area;
}
public Shape(){
}
}
子类:
package com.sun;
public class Rectangle extends Shape {
public Rectangle(double perimeter, double area) {
super(perimeter, area);
// TODO Auto-generated constructor stub
}
}
子类:
package com.sun;
public class Circle extends Shape {
public Circle(double perimeter, double area) {
super(perimeter, area);
// TODO Auto-generated constructor stub
}
}
测试类:
package com.sun;
public class Test {
public static void main(String[] args) {
int x=3; int y=4; int r=5; double π=3.14;
Shape cc=new Rectangle((x+y)*2,x*y);
Shape dd=new Circle(2*π*r,π*r*r);
System.out.println("矩形的周长和面积为:"+cc.getPerimeter()+
"厘米"+" "+cc.getArea()+"平方米");
System.out.println("圆形的周长和面积为:"+dd.getPerimeter()+
"厘米"+" "+dd.getArea()+"平方米");
}
}