老师,剩下的我应该怎么改继承(下)4-3编程

老师,剩下的我应该怎么改继承(下)4-3编程

public class Test {

    public static void main(String[] args) {

// 实例化2个父类对象,传入两组相同的参数值

         Fruits one=new Fruits();

         one.eat();

         Fruits fru1=new Fruits("hehe", "haha");

         Fruits fru2=new Fruits("hehe", "haha");

// 调用父类eat方法

// 测试重写equals方法,判断两个对象是否相等

System.out.println("————————————————————————————————————————");

// 实例化子类对象,并传入相关参数值

   Waxberry two=new Waxberry();

   two.setColor("紫红色");

   two.setShape("圆形");

  // 调用子类face方法和eat方法

   two.face();

   two.eat();

        System.out.println(one.toString());

// 测试重写toString方法,输出子类对象的信息

System.out.println("——————————————————————————————————————————————");

// 实例化Banana类对象,并传入相关参数值

Banana three=new Banana();

three.setVariety("仙人蕉");

three.setShape("稍圆");

three.advantage();

three.advantage("黄色");

// 调用子类的advantage和它的重载方法

}

}

public class Fruits {

    // 私有属性:水果的形状(shape)和口感(taste)

private String shape;

private String taste;

public Fruits() {

}

public Fruits(String shape,String taste){

   this.setShape(shape);

   this.setTaste(taste);

}

public void setShape(String shape){

      this.shape=shape;

}

public String getShape(){

      return this.shape;

}

public void setTaste(String taste){

      this.taste=taste;

}

public String getTaste(){

      return this.taste;

}

// 带参构造函数(参数为shape和taste)

    //通过封装实现对私有属性的get/set访问

    // 创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)

    public void eat(){

        System.out.println("水果可供人们食用!");

    }

// 重写equals方法,比较两个对象是否相等(比较shape,taste)

public boolean equals(Fruits obj){

             if(obj==null)

                return false;

            // Fruits fruit=(Fruits)obj;

             if(this.getShape().equals(obj.getShape())&&this.getTaste().equals(obj.getTaste()))

                return true;

                else

                    return false;

         }

}

final public class Waxberry extends Fruits {

    // 私有属性:颜色(color)

    private String color;

//创建构造方法,完成调用父类的构造方法,完成属性赋值

public Waxberry(){

   

}

public Waxberry(String color,String shape,String taste){

      super(shape,taste);

      this.setColor(color);

}

    public void setColor(String color){

           this.color=color;

    }

    public String getColor(){

           return this.color;

    }

    final public String face(){

          

           String str=("杨梅:"+this.getColor()+"、"+this.getShape()+",果味酸甜适中");

          return str;

    }

    //创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中

    public void eat(){

           System.out.println("杨梅酸甜适中,非常好吃!");

    }

    

    //重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!

    public String toString(){

           return "果实为:"+this.getShape()+"、"+this.getColor()+","+this.getTaste();

    }

    

    //重写toString方法,输出的表现形式不同(输出shape,color,taste)

}

public class Banana extends Fruits{

    // 私有属性:品种(variety)

    private String variety;

    

    public Banana(){

        

    }

    public Banana(String variety){

        this.setVariety(variety);

    }

    public void setVariety(String variety){

           this.variety=variety;

    }

    public String getVariety(){

           return this.variety;

    }

    //创建带参构造方法为所有属性赋值

    //创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。

    public void advantage(){

        System.out.println(this.getVariety()+"果形"+this.getShape()+",果肉香甜,可供生食");

        }

        public void advantage(String color){

           System.out.println(this.getVariety()+"颜色为:"+color);

    }

    //创建重载advantage方法(带参数color),描述为:**颜色为**

    }


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

首先,杨梅和香蕉都继承父类水果(Fruits)写的没问题。但还有不完整的地方:1)未输出fru1.equals(fru2)两个水果是否相同。2)杨梅,toString()中缺少eat()。3)没有输出杨梅的face()。祝:学习愉快

  • 慕九州7737153 提问者 #1
    嗯嗯,所以应该怎么改,就是问这个,在线等
    2018-06-11 16:27:25
  • 好帮手慕雪 回复 提问者 慕九州7737153 #2
    1) 测试重写equals方法,判断两个对象是否相等: System.out.println("两个水果的引用比较"+fru1.equals(fru2)); 2)杨梅,toString()中把this.getTaste()改成this.eat()。3)face()中增加System.out.println(str)
    2018-06-11 16:57:15
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师