这段代码在eclipse编译器中运行正常,但在在线开发环境中报错

来源:4-3 编程练习

weixin_慕设计1308382

2019-07-09 18:15:09

package com.imooc.exercise6;

public final class Waxberry extends Fruits {
	private String color="紫红色";

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	//创建构造方法,完成调用父类的构造方法,完成属性赋值
	public Waxberry() {
		
	}
	public Waxberry(String color) {
		super("圆形","酸甜适中");
	}
    //创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中
	public final void face() {
		System.out.println("杨梅:"+this.getColor()+"、"+super.getShape()+",果味"+super.getTaste());
	}
    //重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
	public void eat() {
		System.out.println("杨梅酸甜适中,非常好吃!");
	}
	  //重写toString方法,输出的表现形式不同(输出shape,color,taste)
	public String toString() {
		return "杨梅的信息:果实为"+super.getShape()+"、"+this.getColor()+","+super.getTaste()+",非常好吃";
	}
}
package com.imooc.exercise6;

public class Banana extends Fruits{
	   // 私有属性:品种(variety)
	private String variety;

	public String getVariety() {
		return variety;
	}

	public void setVariety(String variety) {
		this.variety = variety;
	}
	   //创建带参构造方法为所有属性赋值
	public Banana() {
		
	}
	public Banana(String vatriety) {
		super("短而稍圆","香甜");
		this.setVariety(vatriety);
	}
	   //创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
	public void advantage() {
		System.out.println(this.getVariety()+"果形"+super.getShape()+",果肉香甜,可供生食。");
	}
	   //创建重载advantage方法(带参数color),描述为:**颜色为**
	public void advantage(String color) {
		System.out.println(this.variety+"颜色为"+color);
	}
}
package com.imooc.exercise6;

public class Fruits {
	 // 私有属性:水果的形状(shape)和口感(taste)
	private String shape="";
	private String 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 Fruits() {
		
	}
	// 带参构造函数(参数为shape和taste)
	public Fruits(String shape,String taste) {
		this.setShape(shape);
		this.setTaste(taste);
	}
	// 创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
	public void eat() {
		System.out.println("水果可供人们食用!");
	}
	// 重写equals方法,比较两个对象是否相等(比较shape,taste)
	public boolean equals(Fruits one) {
		if(one==null) {
			return false;
		}
		if(this.getShape().equals(one.getShape())&&this.getTaste().equals(one.getTaste())) {
			return true;
		}else {
			return false;
		}
	}
}
package com.imooc.exercise6;

public class Test {

	public static void main(String[] args) {
		// 实例化2个父类对象,传入两组相同的参数值
		Fruits one=new Fruits();
		// 调用父类eat方法
		one.eat();
		// 测试重写equals方法,判断两个对象是否相等
		System.out.println("fru1和fru2的引用比较:"+one.equals(one));
		System.out.println("————————————————————————————————————————");
		// 实例化子类对象,并传入相关参数值
		Waxberry two=new Waxberry("紫红色");
		// 调用子类face方法和eat方法
		two.face();
		two.eat();
		// 测试重写toString方法,输出子类对象的信息
		System.out.println(two.toString());
		System.out.println("——————————————————————————————————————————————");
		// 实例化Banana类对象,并传入相关参数值
		Banana three=new Banana("仙人蕉");
		three.advantage();
		three.advantage("黄色");
	}

}
问题描述:这段代码在eclipse编译器中运行正常,但在在线开发环境中报错,同时关于fru1和fru2的引用比较是否有更好的方法,能否就代码举个例子?


写回答

1回答

好帮手慕小班

2019-07-09

        同学你好,1、复制运行这段代码在在线编译器中,运行没有问题哦

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

同学报错是不是在编辑器中也将包的内容复制过来了呐?

    2、建议同学将报错信息截图贴出来哦!便于老师定位问题

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

0

0 学习 · 11489 问题

查看课程