代码没有错误,但一直运行不出来

代码没有错误,但一直运行不出来

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("黄色");
 }
}


正在回答

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

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;
		}

修改后的效果图如下:

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

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

好帮手慕小班 2019-04-22 17:38:09

同学,你好 你的代码运行是没有问题的,可能是电脑网络的其他原因导致的,建议重启一下可能就可以了。  复制同学的代码,运行效果如下:

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

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

  • 提问者 Adopat #1
    好的,我试一下,equals 方法写错了吗?怎么结果是不一样的啊,效果图是True,能够指点一下吗?谢谢!!
    2019-04-22 17:51:29
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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