先写出抽象类
/**
* 图形类,抽象类
* @author Administrator
*
*/
public abstract class Shape {
//计算图形周长方法
public abstract double calcRound();
//计算图形面积方法
public abstract double calcArea();
}
再写出下面被继承的子类方法,通过重写父类的方法继承
//圆类,继承图形类
public class Yuan extends Shape{
//特定属性-半径
private double r;
public Yuan(double r){
this.r = r;
}
//周长
@Override
public double calcRound(){
return 2 * Math.PI *r;
}
//面积
@Override
public double calcArea(){
return Math.PI * r * r;
}
}
//三角形类
public class Sanjiaoxing {
private double borderA;
private double borderB;
private double borderC;//底边
private double height;//高
public Sanjiaoxing(double borderA, double borderB, double borderC, double height) {
super();
this.borderA = borderA;
this.borderB = borderB;
this.borderC = borderC;
this.height = height;
}
@Override
public double calcRound(){
return borderA + borderB + borderC;
}
@Override
public double calcArea(){
return borderC * height / 2;
}
}
//矩形类
public class JuXing extends Shape{
private double width;
private double height;
public JuXing(double width, double height) {
super();
this.width = width;
this.height = height;
}
@Override
public double calcRound() {
return (width + height) * 2;
}
@Override
public double calcArea() {
return width * height;
}
}
//测试类
public class Test {
public static void main(String[] args) {
Sanjiaoxing s = new Sanjiaoxing(5,4,3,4);
System.out.println(s.calcRound());
System.out.println(s.calcArea());
Yuan y = new Yuan(5);
System.out.println(y.calcRound());
System.out.println(y.calcArea());
JuXing j = new JuXing(5,10);
System.out.println(j.calcRound());
System.out.println(j.calcArea());
}
}