为什么我的运行结果是false
public class Fruits {
// 私有属性:水果的形状(shape)和口感(taste)
private String name;
private String shape;
private String taste;
public Fruits() {
}
// 带参构造函数(参数为shape和taste)
public Fruits(String shape, String taste) {
this.setShape(shape);
this.setTaste(taste);
}
// 通过封装实现对私有属性的get/set访问
public String getTaste() {
return taste;
}
public void setTaste(String taste) {
this.taste = taste;
}
public void setShape(String shape) {
this.shape = shape;
}
public String getShape() {
return shape;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
public void eat() {
System.out.println("水果可供人食用!");
}
// 重写equals方法,比较两个对象是否相等(比较shape,taste)
public boolean eauals(Object obj) {
if (obj == null)
return false;
Fruits temp = (Fruits) obj;
if (this.getShape().equals(temp.getShape())
&& (this.getTaste() == temp.getTaste()))
return true;
else
return false;
}
}
public final class Waxberry extends Fruits{
// 私有属性:颜色(color)
private String color;
// 创建构造方法,完成调用父类的构造方法,完成属性赋值
public Waxberry(){
super();
}
// 创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中
public final void face(){
System.out.println("杨梅:"+this.getColor()+"、"+this.getShape()+"果味酸甜适中");
}
// 重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
public void eat() {
System.out.println("杨梅酸甜适中,非常好吃!");
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// 重写toString方法,输出的表现形式不同(输出shape,color,taste)
public String toString(){
return this.getName()+"的信息:果实为"+this.getShape()+ "、"+this.getColor()+ ",酸甜适中,非常好吃!";
}
}37
收起
正在回答 回答被采纳积分+1
5回答
喜欢做梦的鱼
2017-03-17 10:29:03
public boolean eauals(Object obj) {
if (obj == null)
return false;
Fruits temp = (Fruits) obj;
if (this.getShape().equals(temp.getShape()) && (this.getTaste() == temp.getTaste()))
return true;
else
return false;
}在你的这段代码中,针对 String taste 应该通过equals方法进行比较,而不是直接通过 == ,调整一下试试看。
如果解决了你得疑惑,欢迎采纳~~
迟人
2017-03-16 21:20:12
package com.imooc.practice02;
public class Banana extends Fruits{
// 私有属性:品种(variety)
private String variety;
public Banana(){
}
//创建带参构造方法为所有属性赋值
public Banana(String variety){
}
//创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
public void advantage(){
System.out.println(this.getName()+"果形"+this.getShape()+",果肉香甜,可供生食。");
}
//创建重载advantage方法(带参数color),描述为:**颜色为**
public void advantage(String color){
System.out.println(this.getName()+"颜色为"+color);
}
}
package com.imooc.test;
import com.imooc.practice02.Banana;
import com.imooc.practice02.Fruits;
import com.imooc.practice02.Waxberry;
public class Test4 {
public static void main(String[] args) {
// 实例化2个父类对象,传入两组相同的参数值
Fruits fru1 = new Fruits("圆形", "甜");
Fruits fru2 = new Fruits("圆形", "甜");
// 调用父类eat方法
fru1.eat();
// 测试重写equals方法,判断两个对象是否相等
boolean flag = fru1.equals(fru2);
System.out.println("fru1和fru2的引用比较:" + flag);
System.out.println("————————————————————————————————————————");
// 实例化子类对象,并传入相关参数值
Waxberry wa = new Waxberry();
// 调用子类face方法和eat方法
wa.setName("杨梅");
wa.setShape("圆形");
wa.setColor("紫红色");
wa.face();
wa.eat();
// 测试重写toString方法,输出子类对象的信息
System.out.println(wa);
System.out.println("——————————————————————————————————————————————");
// 实例化Banana类对象,并传入相关参数值
Banana ba = new Banana();
// 调用子类的advantage和它的重载方法
ba.setName("仙人蕉");
ba.setShape("短而稍圆");
ba.advantage();
ba.advantage("黄色");
}
}
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程



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