练习打卡~请老师检查~

练习打卡~请老师检查~

package hierarchyExercise2_9;

public class Fruits {

	// 私有属性:水果的形状(shape)和口感(taste)
    private String shape;
    private String taste;
    
	// 无参构造方法
	public Fruits() {
       this(null,null);
	}
	// 带参构造函数(参数为shape和taste)
    public Fruits(String shape, String taste) {
    	this.setShape(shape);
    	this.setTaste(taste);
    }
    
    // 通过封装实现对私有属性的get/set访问
	public String getShape() {
		return this.shape;
	}

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

	public String getTaste() {
		return this.taste;
	}

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

	// 创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
    public void eat() {
    	String str = "水果可供人们食用!";
    	System.out.println(str);
    }
	// 重写equals方法,比较两个对象是否相等(比较shape,taste)
    public boolean equals(Fruits fruit) {
    	if(fruit==null) {
    		return false;
    	}
    	if(this.getShape().equals(fruit.getShape())&&(this.getTaste().equals(fruit.getTaste()))) {
    		return true;
    	}
    	else {
		return false;
    	}
    }
}

package hierarchyExercise2_9;

public class Waxberry extends Fruits {

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

	// 无参构造方法
	public Waxberry() {
		this(null, null, null);
	}

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

	// 通过封装实现对私有属性的get/set访问
	public String getColour() {
		return this.colour;
	}

	public void setColour(String colour) {
		this.colour = colour;
	}

	// 创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中
    public final void face() {
		String str = "杨梅:" + this.getColour() + "、" + this.getShape() + ",果味酸甜适中";
		System.out.println(str);
	}

	// 重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
	public void eat() {
		String str = "杨梅酸甜适中,非常好吃!";
		System.out.println(str);
	}

	// 重写toString方法,输出的表现形式不同(输出shape,color,taste)
	public String toString() {
		String str = "杨梅的信息:果实为" + this.getShape() + "," + this.getColour() + "," + this.getTaste() + ",非常好吃!";
		return str;
	}

}

package hierarchyExercise2_9;
import hierarchyExercise2_9.Waxberry;

public class Banana extends Fruits {

    // 私有属性:品种(variety)
    private String variety;
    
    //无参构造方法
    public Banana() {
    	this(null,null);
    }
	// 创建带参构造方法为所有属性赋值
    public Banana(String variety, String shape) {
    	this.setVariety(variety);
    	this.setShape(shape);
    }
    
    // 通过封装实现对私有属性的get/set访问
	public String getVariety() {
		return this.variety;
	}
	public void setVariety(String variety) {
		this.variety = variety;
	}
    
	// 创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
    public void advantage() {
    	String str = this.getVariety() + "" + this.getShape() + ",果肉香甜,可供生食。";
    	System.out.println(str);
    }
	// 创建重载advantage方法(带参数color),描述为:**颜色为**
    public String advantage(String colour) {
    	String str = this.getVariety() + "颜色为" + colour;
    	return str;
    }
}

package hierarchyExercise2_9;

public class Test {

	public static void main(String[] args) {
		// 实例化2个父类对象,传入两组相同的参数值
        Fruits fruit1 = new Fruits("圆形","果味酸甜适中");
        Fruits fruit2 = new Fruits("圆形","果味酸甜适中");
		// 调用父类eat方法
        fruit1.eat();
		// 测试重写equals方法,判断两个对象是否相等
        System.out.println("fruit1和fruit2的引用比较:" + fruit1.equals(fruit2));
		System.out.println("————————————————————————————————————————");
		// 实例化子类对象,并传入相关参数值
        Waxberry waxberry = new Waxberry("圆形", "酸甜适中", "紫红色");
		// 调用子类face方法和eat方法
        waxberry.face();
        waxberry.eat();
		// 测试重写toString方法,输出子类对象的信息
        System.out.println(waxberry.toString());
		System.out.println("——————————————————————————————————————————————");
		// 实例化Banana类对象,并传入相关参数值
        Banana banana = new Banana("仙人蕉", "短而稍圆");
		// 调用子类的advantage和它的重载方法
        banana.advantage();
        System.out.println(banana.advantage("黄色"));
	}
}

老师,请问怎么使Waxberry这个不能再成为别的类的父类呢?

正在回答

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

1回答

public class Waxberry的class之前加关键字final

  • Heijyu 提问者 #1
    好的!谢谢~刚好在练习完的下一章看到了关于final的内容了~
    2020-05-24 10:30:39
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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