【JAVA 课后习题 13.12】

自己老是给自己找麻烦~~很快写好代码后~却发了很长时间找自己的Bug~
好想找个人能和自己一起敲JAVA~一起交流~

GeOb类代码~(加了两个构造方法)

public abstract class GeOb{
    private String color = "white";
    private boolean filled;
    protected java.util.Date da;
    int a,b,r;
    public void setBian(int a,int b){
        this.a = a;
        this.b = b;
    }
    public void setBan(int r){
            this.r = r;
    }
    protected GeOb(){
        da = new java.util.Date();
    }
    protected GeOb(String color,boolean filled){
        da = new java.util.Date();
        this.color = color;
        this.filled = filled;
    }
    public void setGe(String color,boolean filled){
        da = new java.util.Date();
        this.color = color;
        this.filled = filled;
    }
    public String getcolor(){
        return color;
    }
    public void setcolor(String color){
        this.color = color;
    }
    public boolean getfillrd(){
        return this.filled;
    }
    public void setfilled(boolean filled){
        this.filled = filled;
    }
    public java.util.Date getda(){
        return da;
    }
    public String toString(){
        return "created on " + da + "\ncolor : " + color + " and filled : " + filled;
    }
    public void howToColor() {
        System.out.println("Color all four sides ");
    }
    interface Colorable{
        public void howToColor();
    }
    protected abstract double getArea();
    protected abstract double getPerimeter();
}

JuXi子类代码~

public class JuXi extends GeOb{

     protected double getArea() {
        return this.a * this.b;
    }

     protected double getPerimeter() {
        return 2 * a * b;
    }
}

Yuan子类代码~

public class Yuan extends GeOb{

    protected double getArea(){
        return Math.PI * this.r * this.r;
    }

     protected double getPerimeter() {
        return 2 * Math.PI * this.r * 1.0;
    }
}

Text实现代码~~

public class Text2 {

    public static void main(String[] args) {
        GeOb[] a = new GeOb[4];
        a[0] = new JuXi(); // 数组里的元素要逐个实例化
        a[0].setBian(1, 2);
        a[1] = new JuXi();
        a[1].setBian(3, 5);
        a[2] = new Yuan();
        a[2].setBan(2);
        a[3] = new Yuan();
        a[3].setBan(7); 
        System.out.println(sumArea(a));
    }
    public static double sumArea(GeOb[] a){ // 题目要求的构造方法
        double sum = 0.0;
        for(int i = 0 ; i < a.length ; i++)
            sum += a[i].getArea();
        return sum;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值