/**
* 图形
*/
public abstract class Graph {
//计算周长
public abstract void perimeter();
//计算面积
public abstract void area();
}
/**
* 长方形
*/
public class Rectangle extends Graph {
private double wide;
private double high;
public Rectangle() {
}
public Rectangle(double wide, double high) {
this.wide = wide;
this.high = high;
}
//计算周长
@Override
public void perimeter() {
System.out.println("宽为"+this.wide+"高为"+this.high+"的长方形的周长为"+2*(this.wide+this.high));
}
//计算面积
@Override
public void area() {
System.out.println("宽为"+this.wide+"高为"+this.high+"的长方形的周长为"+this.wide*this.high);
}
public double getWide() {
return wide;
}
public void setWide(double wide) {
this.wide = wide;
}
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
}
/**
* 圆形
*/
public class Round extends Graph{
private double r;
static final double PI=3.14;
public Round() {
}
public Round(double r) {
this.r = r;
}
//计算周长
@Override
public void perimeter() {
System.out.println("半径为"+r+"的圆的周长为"+2*PI*r);
}
//计算面积
@Override
public void area() {
System.out.println("半径为"+r+"的圆的周长为"+PI*r*r);
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
public class Test {
public static void main(String[] args) {
System.out.println("--------长方形--------");
Graph rectangle = new Rectangle(2,3);
rectangle.perimeter();
rectangle.area();
System.out.println("--------圆形--------");
Graph round = new Round(2);
round.perimeter();
round.area();
}
}