public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle shape1 = new Rectangle();
Circle shape2 = new Circle();
shape1.length = 12.0;
double peri1 = shape1.getPerimeter();
shape2.radius = 1.0;
double peri2 = shape2.getPerimeter();
System.out.println("shape1正方形的周长:" + peri1);
System.out.println("shape2圆的周长" + peri2);
}
}
public abstract class Shape {
public abstract void setRadius(double radius);
public abstract double getPerimeter();
public abstract void setLength(double length);
}
public class Circle extends Shape {
public double radius;
public void setRadius(double radius) {
// TODO Auto-generated method stub
this.radius = radius;
}
public double getPerimeter() {
// TODO Auto-generated method stub
return 2 * radius * Math.PI;
}
public void setLength(double length) {
// TODO Auto-generated method stub
}
}
public class Rectangle extends Shape {
public double length;
public void setLength(double length) {
// TODO Auto-generated method stub
this.length = length;
}
public double getPerimeter() {
// TODO Auto-generated method stub
return 4 * length;
}
public void setRadius(double radius) {
// TODO Auto-generated method stub
}
}