代码没有错误,但一直运行不出来
package com.imooc.fruit;
public class Fruits {
private String shape;
private String taste;
public Fruits() {
}
public Fruits(String shape, String taste) {
this.shape = shape;
this.taste = taste;
}
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;
}
public void eat() {
System.out.println("水果可以供人们食用!");
}
public boolean equals(Object obj) {
if (obj != null && obj.getClass() == this.getClass()) {
Fruits other = (Fruits) obj;
if (other.getShape() == null || shape == null || other.getTaste() == null || taste == null) {
return false;
} else if (other.getShape().equals(shape) && other.getTaste().equals(taste)) {
return true;
}
}
return true;
}
}
package com.imooc.fruit;
final class Waxberry extends Fruits {
private String color;
public Waxberry() {
super();
}
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;
}
public final void face() {
setColor("红色");
System.out.println("杨梅:"+this.getColor()+"、"+this.getShape()+"、"+this.getTaste());
}
public void eat() {
System.out.println("杨梅酸甜适中非常好吃!");
}
public String toString() {
return "杨梅的信息:果实为:"+this.getShape()+this.getColor()+this.getTaste()+"非常好吃";
}
}
package com.imooc.fruit;
public class Banana extends Fruits {
private String variety;
public Banana() {
}
public Banana(String variety, String shape, String taste) {
super(shape, taste);
this.variety = variety;
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public void advantage() {
System.out.println(this.getVariety() + "果形" + this.getShape() + "果肉香甜,可供生食");
}
public void advantage(String color) {
System.out.println(this.variety + "颜色为" + color);
}
}
package com.imooc.fruit;
public class Test {
public static void main(String[] args) {
Fruits fruit1 = new Fruits();
Fruits fruit2 = new Fruits();
fruit1.eat();
boolean flag = fruit1.equals(fruit2);
System.out.println("fruit1和fruit2的引用比较:" + flag);
System.out.println("--------------------------------------");
Waxberry berry = new Waxberry("紫红色", "圆形", "口味酸甜适中");
berry.face();
berry.eat();
System.out.println(berry.toString());
System.out.println("-----------------------------");
Banana banana = new Banana("仙人蕉果", "短而稍圆", "果肉香甜,可供生食");
banana.advantage();
banana.advantage("黄色");
}
}0
收起
正在回答
2回答
同学,你好 问题是重写的equals方法有错误,建议重新写一下equals方法,例如:
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Fruits other = (Fruits) obj;
if (shape == null) {
if (other.shape != null)
return false;
} else if (!shape.equals(other.shape))
return false;
if (taste == null) {
if (other.taste != null)
return false;
} else if (!taste.equals(other.taste))
return false;
return true;
}修改后的效果图如下:

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星