请老师检查一下我的代码,谢谢。
来源:4-3 编程练习
嫣语四然
2021-09-26 22:56:52
Fruits 类
package com.immoc.jjicheng4;
public class Fruits {
private String shape;
private String taste;
public Fruits() {
}
public Fruits(String shape, String taste) {
this.setShape(shape);
this.setTaste(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)
return false;
Fruits temp = (Fruits) obj;
if ((this.getShape().equals(temp.getShape())) && (this.getTaste().equals(temp.getTaste())))
return true;
else
return false;
}
}
Waxberry类
package com.immoc.jjicheng4;
final public 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 shape, String taste, String color) {
super(shape, taste);
this.setColor(color);
}
final public void face() {
System.out.println("杨梅:" + this.getColor() +"、" +this.getShape() + ",果味" + this.getTaste()+"。");
}
@Override
public void eat() {
System.out.println("杨梅酸甜适中,非常好吃!");
}
public String toString() {
return "杨梅的信息:果实为" + this.getShape() + this.getColor() + this.getTaste() + "非常好吃!";
}
}
Banana类
package com.immoc.jjicheng4;
public class Banana extends Fruits {
private String variety;
public Banana() {
}
public Banana(String shape, String taste, String variety) {
super(shape,taste);
this.setVariety(variety);
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public void advantage() {
System.out.println(this.getVariety() + "果形" + this.getShape() + "," + this.getTaste() + ",可供生食。");
}
public void advantage(String color) {
System.out.println(this.getVariety() + "颜色为" + color);
}
}
Test类
package com.immoc.jjicheng4;
public class Test {
public static void main(String[] args) {
// 实例化2个父类对象,传入两组相同的参数值
Fruits fru1 = new Fruits("圆形", "甜");
Fruits fru2 = new Fruits("圆形", "甜");
// 调用父类eat方法
fru1.eat();
// 测试重写equals方法,判断两个对象是否相等
System.out.println("fru1和fru2的引用比较:" + fru1.equals(fru2));
System.out.println("————————————————————————————————————————");
// 实例化子类对象,并传入相关参数值
Waxberry one = new Waxberry("圆形", "酸甜适中", "紫红色");
// 调用子类face方法和eat方法
one.face();
one.eat();
// 测试重写toString方法,输出子类对象的信息
one.toString();
System.out.println("——————————————————————————————————————————————");
// 实例化Banana类对象,并传入相关参数值
Banana two = new Banana("短而稍圆", "果肉香甜", "仙人蕉");
// 调用子类的advantage和它的重载方法
two.advantage();
two.advantage("黄色");
}
}
1回答
好帮手慕小尤
2021-09-27
同学你好,已完成练习,棒棒哒!继续加油!
祝学习愉快!
相似问题