老师帮我看看这样写有没有问题
来源:4-3 编程练习
夏蔚海
2020-03-31 19:50:13
package com.imooc.zy4;
import java.util.Objects;
public class Fruits {
//1、根据杨梅和香蕉的共性,抽取父类水果(Fruits)
//私有属性:水果的形状(shape)和口感(taste)
private String shape;
private String taste;
//方法:
//1) 带参构造函数(参数为shape和taste)
public Fruits(){}
public Fruits(String shape, String taste) {
this.shape = shape;
this.taste = 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;
}
//2) 创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
public void eat(){
System.out.println("水果可供人们食用!");
}
//3) 重写equals方法,比较两个对象是否相等(比较shape,taste)
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Fruits fruits = (Fruits) o;
return Objects.equals(shape, fruits.shape) &&
Objects.equals(taste, fruits.taste);
}
}
package com.imooc.zy4;
public class Banana extends Fruits{
//私有属性:品种(variety)
private String variety;
//方法:
//1) 带参构造方法为所有属性赋值
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;
}
//2) 创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
public void advantage(){
System.out.println(this.getVariety() + "果形" + this.getShape() + ",果肉" + this.getTaste() + ",可供生食。");
}
//3) 重载要求(2)中的advantage方法(带参数color),描述为:**颜色为**
public void advantage(String color){
System.out.println(this.getVariety() + "颜色为:" + color);
}
}
package com.imooc.zy4;
public final class Waxberry extends Fruits{
//私有属性:颜色(color)
private String color;
//方法:
//1) 调用父类的构造方法,完成属性赋值
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;
}
//2) 创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中。
public final String face(){
String str = "杨梅:" + this.color + "、" + this.getShape() + ",果味酸甜适中。";
return str;
}
//3) 重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
@Override
public void eat() {
System.out.println("杨梅酸甜适中,非常好吃!");
}
//4) 重写toString方法,输出的表现形式不同(输出shape,color,taste)
@Override
public String toString() {
return "杨梅的信息:果实为" + this.getShape() + "、" + this.color + "," + this.getTaste() + ",非常好吃!";
}
//5) 要求Waxberry类不允许有子类
}
package com.imooc.zy4;
public class Test {
public static void main(String[] args) {
//1) 实例化2个父类对象,并传入两组相同的参数值
Fruits fruits1 = new Fruits("圆形","酸甜适中");
Fruits fruits2 = new Fruits("圆形","酸甜适中");
//2) 调用父类eat方法
fruits1.eat();
//3) 测试重写equals方法,判断两个对象是否相等
System.out.println("fruits1 和 fruits2 的引用比较:" + fruits1.equals(fruits2));
System.out.println("-------------------------------------");
//4) 实例化子类Wacberry对象,并传入相关参数值
Waxberry waxberry = new Waxberry("圆形","酸甜适中","紫红色");
//5) 调用子类face方法和重写父类eat方法后的eat方法
System.out.println(waxberry.face());
waxberry.eat();
//6) 测试重写toString方法,输出子类对象的信息
System.out.println(waxberry.toString());
System.out.println("-------------------------------------");
//7) 实例化Banana类对象,并传入相关参数值
Banana banana = new Banana("短而稍圆","香甜","仙人蕉");
//8) 调用子类的advantage和它的重载方法
banana.advantage();
banana.advantage("黄色");
}
}
1回答
好帮手慕小脸
2020-04-01
同学你好!代码完成符合题目要求,条例清晰。很棒!加油,
祝学习愉快~
相似问题