请老师看看是否达到题目要求

请老师看看是否达到题目要求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
public class Fruits {
    // 私有属性:水果的形状(shape)和口感(taste)
    private String shape;
    private String taste;
 
    public Fruits() {
 
    }
 
    // 带参构造函数(参数为shape和taste)
    public Fruits(String shape, String taste) {
        this.shape = shape;
        this.taste = 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 object) {
        if (object == null)
            return false;
        if (this.getShape() == object.getShape() && this.getTaste() == object.getTaste())
            return true;
        else
            return false;
    }
 
}
 
//要求Waxberry类不允许有子类
public final class Waxberry extends Fruits {
    // 私有属性:颜色(color)
    private String color;
 
    // 创建构造方法,完成调用父类的构造方法,完成属性赋值
    public Waxberry() {
 
    }
 
    public Waxberry(String shape, String taste, String color) {
        super(shape, taste);
        this.color = 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() + ",果味" this.getTaste() + "。");
    }
 
    // 重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
    @Override
    public void eat() {
//                super.eat();
        System.out.println("杨梅酸甜适中,非常好吃!");
    }
 
    // 重写toString方法,输出的表现形式不同(输出shape,color,taste)
    @Override
    public String toString() {
        String str = "杨梅的信息:果实为" this.getShape() + "、" this.getColor() + "," this.getTaste() + "非常好吃!";
        return str;
    }
}
 
public class Banana extends Fruits {
    // 私有属性:品种(variety)
    private String variety;
 
    // 创建带参构造方法为所有属性赋值
    public Banana() {
 
    }
 
    public Banana(String shape, String taste, String variety) {
        super(shape, taste);
        this.variety = variety;
 
    }
 
    public String getVariety() {
        return variety;
    }
 
    public void setVariety(String variety) {
        this.variety = variety;
    }
 
    // 创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
    public void advantage() {
        System.out.println(this.getVariety() + "果形" this.getShape() + this.getTaste() + ",可供生食。");
    }
 
    // 创建重载advantage方法(带参数color),描述为:**颜色为**
    public void advantage(String color) {
        System.out.println( this.variety + "颜色为" + color);
    }
 
}
 
public class Test {
    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 wb=new Waxberry("圆形","酸甜适中","紫红色");
        // 调用子类face方法和eat方法
        wb.face();
        wb.eat();
        // 测试重写toString方法,输出子类对象的信息
        System.out.println(wb.toString());
        System.out.println("——————————————————————————————————————————————");
        // 实例化Banana类对象,并传入相关参数值
        Banana bn=new Banana("短而稍圆","果肉香甜","仙人蕉");
         
        // 调用子类的advantage和它的重载方法
        bn.advantage();
        bn.advantage("黄色");
 
    }
}


正在回答

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

2回答
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    // 重写equals方法,比较两个对象是否相等(比较shape,taste)
    public boolean equals(Fruits object) {
        if (object == null)
            return false;
        if (this.getShape() == object.getShape() && this.getTaste() == object.getTaste())
            return true;
        else
            return false;
    }
 
     Fruits类的父类是Object,Object的equals(Object obj)方法参数是Object类,你这里都把参数改动了。  
     你只是定义了一个equals(Fruits object)的方法,并为重写。
     
    可以看看这个
    // 重写equals方法,比较两个对象是否相等(比较shape,taste)
    @Override
    public boolean equals(Object obj) {
        if (obj == nullreturn false;
        // 强转为Fruits类
        Fruits fruit = (Fruits) obj;
        if (this.getShape().equals(fruit.getShape()) && (this.getTaste().equals(fruit.getTaste()))) {
            return true;
        else {
            return false;
        }
    }    
方法重写:
1.有继承关系的子类中
2.方法名相同,参数列表相同(参数顺序、个数、类型),方法的返回值可以允许是子类类
3.方法的修饰符,重写方法访问权限必须大于等于父类方法。
4.与方法的参数名无关
5.静态方法和最终方法不能被重写;但静态方法在子类中可以通过隐藏父类方法的方式重新实现。


  • wemooc #1
    你只是定义了一个equals(Fruits object)的方法,并未重写Object的equals(Object obj)方法。
    2018-11-08 02:39:39
  • jjboy 提问者 #2
    那么就是这么理解:之前课程视频中说的通过equals(Animal object)限定输入equals方法的是Animal类中的对象,只是解决问题的一种方案而已,而真正的对object类的重写必须Fruits fruit = (Fruits) obj;强制转换,object 也是 Object 类的对象是吧?
    2018-11-08 13:21:47
  • wemooc 回复 提问者 jjboy #3
    是的,你的代码功能上是没有问题的。 重载和重写的概念弄清楚点就好了。
    2018-11-08 13:37:54
提问者 jjboy 2018-11-08 13:11:23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 用于在测试类中检测属性相同的对象实例化是否指向同一个对象的方法
    /*
     * public boolean equals (Object obj) { if(obj==null) return false; Animal
     * temp=(Animal)obj;
     * if(this.getName()==temp.getName()&&this.getMonth()==temp.getMonth()) return
     * true; else return false; }
     */
 
    public boolean equals(Animal object) {
        if (object == null)
            return false;
        if (this.getName() == object.getName() && this.getMonth() == object.getMonth())
            return true;
        else
            return false;
    }


问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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