4-3练习,麻烦老师帮忙看看还有什么不足之处,谢谢!!!!

4-3练习,麻烦老师帮忙看看还有什么不足之处,谢谢!!!!

package fruits;

public class Fruits {
	private String shape;
	private String taste;

	// public Fruits(){}
	// 带参构造函数(参数为shape和taste)
	public Fruits(String shape, String taste) {
		this.setShape(shape);
		this.setTaste(taste);
	}

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

	public String getShape() {
		return shape;
	}

	public void setShape(String shape) {
		this.shape = shape;
	}

	public String getTaste() {
		return taste;
	}

	public void setTaste(String taste) {
		this.taste = taste;
	}

	// 创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
	public void eat() {
		System.out.println("水果可供人们食用!");
	}

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

		if (fru == null) {

			return false;

		}

		if (this.getShape().equals(fru.getShape()) && this.getTaste().equals(fru.getTaste())) {

			return true;

		} else {

			return false;

		}
	}
}
=======================================
package fruits;

public final class Waxberry extends Fruits {
	

	// 私有属性:颜色(color)
    private String color;

		//创建构造方法,完成调用父类的构造方法,完成属性赋值
	      public Waxberry(String color, String shape, String taste) {
		   super(shape, taste);
		this.setColor(color);
	}

	      //get set方法
	    public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	    //创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中
	    
	    final public void face(){
	    	System.out.println("杨梅:"+this.getShape()+"、" +this.getColor()+"果味酸甜适中");
	    }
	    //重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
	    @Override
	    public void eat() {
	    	// TODO Auto-generated method stub
	    	System.out.println("杨梅酸甜适中,非常好吃!");
	    }
	    
	    //重写toString方法,输出的表现形式不同(输出shape,color,taste)
	    @Override
	    public String toString() {
	    	// TODO Auto-generated method stub
	    	return this.getShape()+this.getColor()+this.getTaste();
	    }
}
==========================================
package fruits;

public class Banana extends Fruits {
	// 私有属性:品种(variety)
	private String variety;

	// 创建带参构造方法为所有属性赋值
	public Banana(String shape, String taste, String variety) {
		super(shape, taste);
		this.setVariety(variety);
	}

	// get set方法
	public String getVariety() {
		return variety;
	}

	public void setVariety(String variety) {
		this.variety = variety;
	}

	// 创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
	public void advantage() {
		System.out.println("果形" + this.getShape() + ",果肉香甜,可供生食。");
	}

	// 创建重载advantage方法(带参数color),描述为:**颜色为**
	public void advantage(String color) {
		System.out.println(this.getVariety() + "颜色为" + color);
	}
}
=======================================
package fruits;

public class Test {

	public static void main(String[] args) {
		// 实例化2个父类对象,传入两组相同的参数值
		Fruits fru1 = new Fruits("圆形", "香甜");
		Fruits fru2 = new Fruits("圆形", "香甜");
		// 调用父类eat方法
		fru1.eat();

		// 测试重写equals方法,判断两个对象是否相等
          System.out.println(fru1.equals(fru1));
		System.out.println("————————————————————————————————————————");
		// 实例化子类对象,并传入相关参数值
		Waxberry wax = new Waxberry("红色", "圆型", "香甜");

		// 调用子类face方法和eat方法
		wax.face();
		wax.eat();

		// 测试重写toString方法,输出子类对象的信息
System.out.println(wax.toString());
		System.out.println("——————————————————————————————————————————————");
		// 实例化Banana类对象,并传入相关参数值
		Banana ban=new Banana("长条状", "细腻", "仙人蕉");
		// 调用子类的advantage和它的重载方法
ban.advantage();
ban.advantage("黄色");
	}

}


正在回答

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

1回答

你好!整体完成得很好,有几个小问题如下所示:

1、在箭头所指位置写上fru2.eat();也就是fru2也调用eat()方法

http://img1.sycdn.imooc.com//climg/5d40258e0001599b03660102.jpg

2、应该是fru1和fru2进行比较,将如下选中的fru1改成fru2

http://img1.sycdn.imooc.com//climg/5d4025c200018e9104810054.jpg

3、建议将.toString()去掉,输出对象时,默认会调用toString()方法

http://img1.sycdn.imooc.com//climg/5d4025f10001758e03890052.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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