4-3编程练习

来源:4-3 编程练习

慕后端4084011

2019-10-13 18:33:57

public class Fruits {
	private String shape; //共性形状
	private String taste; //共性口感
	//父类无参构造
	public Fruits() {
		
	}
	//父类带参构造
	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 obj) {
		if(obj==null)
			return false;
		if(this.getShape().equals(getShape())&&(this.getTaste()==obj.getTaste()))
			return true;
		else
			return false;
	}
	
}
public class Waxberry extends Fruits {
	private String color; //特有属性颜色
	//子类无参构造
	public Waxberry() {
		
	}
	//创建构造方法,完成调用父类的构造方法,完成属性赋值
	public Waxberry(String color,String shape,String taste) {
		super("圆形","酸甜适中");
		this.setColor(color);
	}
	
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	
	//创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中
	public final String face() {
		return "杨梅:"+this.getColor()+"、"+this.getShape()+",果味酸甜适中。";
	}
	
	//重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
    @Override
    public void eat() {
    	System.out.println("杨梅酸甜适中,非常好吃!");
    }
    
    //重写toString方法,输出的表现形式不同(输出shape,color,taste)
	public String toString() {
		return "杨梅的信息:果实为"+this.getShape()+"、"+this.getColor()+","+this.getTaste()+",非常好吃!";
	}	
	
}
public class Banana extends Fruits {
	// 私有属性:品种(variety)
    private String variety;
    
    public Banana() {
    	
    }
    
    //创建带参构造方法为所有属性赋值
    public Banana(String variety,String shape) {
    	this.setShape(shape);
    	this.setVariety(variety);
    }

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

    //创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
    public void advantage() {
    	System.out.println(this.getVariety()+"果形"+this.getShape()+",果肉香甜,可供生食。");
    }
    
    //创建重载advantage方法(带参数color),描述为:**颜色为**
    public void advantage(String color) {
    	System.out.println(this.getVariety()+"颜色为"+color);
    }
   
}
public class Test {

	public static void main(String[] args) {
		// 实例化2个父类对象,传入两组相同的参数值
		Fruits fru1=new Fruits("形状","口感");
		Fruits fru2=new Fruits("形状","口感");

		// 调用父类eat方法
		fru1.eat();		

		// 测试重写equals方法,判断两个对象是否相等
		boolean flag=fru1.equals(fru2);
		System.out.println("fru1和fru2的引用比较:"+flag);
		System.out.println("————————————————————————————————————————");
		
		// 实例化子类对象,并传入相关参数值
		Waxberry one = new Waxberry("紫红色","圆形","酸甜适中")	;

		// 调用子类face方法和eat方法
		System.out.println(one.face());
		one.eat();

		// 测试重写toString方法,输出子类对象的信息
		System.out.println(one);

		System.out.println("——————————————————————————————————————————————");
		// 实例化Banana类对象,并传入相关参数值
		Banana ban=new Banana("仙人蕉","短而稍圆");

		// 调用子类的advantage和它的重载方法
		ban.advantage();
		ban.advantage("黄色");	
	}

}

麻烦老师看下代码有没有需要优化的地方?

写回答

1回答

好帮手慕小班

2019-10-13

同学你好,这里复制运行贴出代码,与题目要求效果一致,很棒呐,但是有一个小小的建议:

这里taste属性是String类型的属性,所以在equals方法中不建议使用==来比较两个属性

http://img.mukewang.com/climg/5da303480999145408230239.jpg

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

0

0 学习 · 11489 问题

查看课程