老师,请问错误在哪里呀
来源:4-3 编程练习
慕仙7354812
2020-01-02 20:01:37
public class Fruits {
// 私有属性:水果的形状(shape)和口感(taste)
private String shape;
private String taste;
//
public Fruits() {
}
// 带参构造函数(参数为shape和taste)
public Fruits(String schape, String tatste) {
this.setShape(shape);
this.setTaste(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 fruits) {
if (fruits==null) {
return false;
}
if(this.getShape().equals(fruits.getShape())&&this.getTaste().equals(fruits.getTaste())){
return true;
}else {
return false;
}
}
}
public final class Waxberry extends Fruits{
// 私有属性:颜色(color)
private String color;
//创建构造方法,完成调用父类的构造方法,完成属性赋值
public Waxberry() {
}
public Waxberry(String color,String shape,String taste) {
super("圆形","酸甜适中");
this.setColor(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()+",果味酸甜适中");
}
//重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
public void eat() {
System.out.println("杨梅酸甜适中,非常好吃!");
}
//重写toString方法,输出的表现形式不同(输出shape,color,taste)
public String toString() {
return "杨梅的信息:果实为"+this.getShape()+","+this.getColor()+","+this.getTaste()+"非常好吃!";
}
}
public class Banana extends Fruits{
// 私有属性:品种(variety)
private String variety;
//private String color;
//创建带参构造方法为所有属性赋值
public Banana() {
}
//
public Banana(String variety) {
this.setVariety(variety);
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
//public String getColor() {
// return color;
//}
//public void setColor(String color) {
// this.color = color;
//}
//创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
public void advantage() {
System.out.println(this.getVariety()+"果形"+this.getShape()+",果肉香甜,可供生食。");
}
//创建重载advantage方法(带参数color),描述为:**颜色为**
public void advantage(String variety,String color) {
System.out.println(this.getVariety()+"颜色为"+color);
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 实例化2个父类对象,传入两组相同的参数值
Fruits fru1=new Fruits("圆形","酸甜");
Fruits fru2=new Fruits("圆形","酸甜");
// 调用父类eat方法
fru1.eat();
// 测试重写equals方法,判断两个对象是否相等
boolean b=fru1.equals(fru2);
System.out.println("fru1和fru2的引用比较:"+b);
System.out.println("————————————————————————————————————————");
// 实例化子类对象,并传入相关参数值
Waxberry wax=new Waxberry();
wax.setColor("紫红色");
wax.setShape("圆形");
wax.setTaste("酸甜适中");
// 调用子类face方法和eat方法
wax.face();
wax.eat();
// 测试重写toString方法,输出子类对象的信息
System.out.println(wax.toString());
System.out.println("——————————————————————————————————————————————");
// 实例化Banana类对象,并传入相关参数值
Banana banana=new Banana();
banana.setVariety("仙人蕉");
//banana.setColor("黄色");
banana.setShape("短而稍圆");
// 调用子类的advantage和它的重载方法
banana.advantage();
banana.advantage("仙人蕉","黄色");
}
}2回答
同学你好,下面是eclipse中的控制台报错提示,点击如下选中的内容,会跳转到代码中。

发现出错的是下面蓝色背景的代码。之所以出现空指针,是因为既有值为null的对象调用方法或属性导致的,错误出现在了 Fruits类中40行。可以看出shape和taste并没有赋值,所以依然是默认的值null。

根据根本原因去追踪找错,会发现赋值时,构造中的参数传递与set中的不一致 。如下图所示:

建议同学以后把错误信息截图上来,方便老师解决问题
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
ByteDancer07
2020-01-02
你把错误信息也粘贴上来啊。。
相似问题
回答 1
回答 1