老师代码报错有问题,但不知道要咋处理,烦请帮助下,谢谢。
来源:4-3 编程练习
Co2渔夫
2019-12-26 11:06:08
public class Fruits {
// 私有属性:水果的形状(shape)和口感(taste)
private String shape;
private String taste;
public Fruits() {
}
// 带参构造函数(参数为shape和taste)
public Fruits(String shape, String taste) {
this.shape = shape;
this.taste = 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(obj.getShape()) && this.getTaste().equals(obj.getTaste())) {
return true;
}
return false;
}
}
---------------------------------------------------------------------------------------------------
// 要求Waxberry类不允许有子类
public class Waxberry {
// 私有属性:颜色(color)
private String color;
public Waxberry() {
}
// 创建构造方法,完成调用父类的构造方法,完成属性赋值
public Waxberry(String color, String shape, String taste) {
super(shape, taste);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// 创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中
public final void face() {
System.out.println("杨梅" + this.getColor() + "、" + this.getShape() + "果味酸甜适中" + this.getColor());
}
// 重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
public void eat() {
System.out.println("杨梅" + this.getTaste);
}
// 重写toString方法,输出的表现形式不同(输出shape,color,taste)
@Override
public String toString() {
String str = "杨梅的信息:果实为" + super.getShape() + "、" + this.getColor() + "," + super.getTaste() + ",非常好吃!";
return str;
}
}
------------------------------------------------------------------------------------------
public class Banana {
// 私有属性:品种(variety)
private String variety;
public Banana() {
}
// 创建带参构造方法为所有属性赋值
public Banana(String variety, String color, String shape, String taste) {
super(shape, taste);
this.variety = variety;
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
// 创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
public void advantage() {
System.out.println(this.getVariety() + "果形" + super.getShape() + super.getTaste() + ",可供生食。");
}
// 创建重载advantage方法(带参数color),描述为:**颜色为**
public void advantage(String color) {
System.out.println(this.getVariety() + "颜色为" + color);
}
}
--------------------------------------------------------------------------------------
public class Test {
public static void main(String[] args) {
// 实例化2个父类对象,传入两组相同的参数值
Fruits one = new Fruits("圆形", "酸甜适中");
Fruits two = new Fruits("圆形", "酸甜适中");
// 调用父类eat方法
one.eat();
// 测试重写equals方法,判断两个对象是否相等
boolean flag = one.equals(two);
System.out.println("f1和f2的引用比较:" + flag);
System.out.println("————————————————————————————————————————");
// 实例化子类对象,并传入相关参数值
Waxberry wa = new Waxberry("紫红色", "圆形", "酸甜适中");
// 调用子类face方法和eat方法
wa.face();
wa.eat();
// 测试重写toString方法,输出子类对象的信息
System.out.println(wa.toString());
System.out.println("——————————————————————————————————————————————");
// 实例化Banana类对象,并传入相关参数值
Banana ba=new Banana("短而稍圆","果肉香甜","仙人焦");
// 调用子类的advantage和它的重载方法
ba.advantage();
ba.advantage("黄色");
}
}
3回答
好帮手慕小脸
2019-12-26
同学你好,在同学的Waxberry类跟Banana类中没有继承父类Fruits,修改如下图所示:
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
Co2渔夫
提问者
2019-12-26
现在的主要编码报错是这里。
好帮手慕小脸
2019-12-26
同学你好,错误原因有如下几点:
1、在Waxberry类中,重写eat方法描述杨梅应该是this.getTaste()而不是getTaste。
2、在Banana类中,有参构造的参数与Test类中的Banana对象的传参个数不一致
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题
回答 2