package fruits;
public class Fruits {
private String shape;
private String 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 Fruits() {
super();
}
public Fruits(String shape, String taste) {
super();
this.setShape(shape);
this.setTaste(taste);
}
public void eat() {
System.out.println("水果可供人们食用!");
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
} else {
Fruits fruits = (Fruits) obj;
if (this.getShape().equals(fruits.getShape())
&& this.getTaste().equals(fruits.getTaste())) {
return true;
} else {
return false;
}
}
}
}
package fruits;
public final class Waxberry extends Fruits {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Waxberry() {
super();
}
public Waxberry(String shape, String taste, String color) {
super(shape, taste);
this.setColor(color);
}
public final void face() {
System.out.println("杨梅:" + this.getColor() + "、" + this.getShape()
+ "," + "果味" + this.getTaste() + "。");
}
@Override
public void eat() {
System.out.println("杨梅酸甜适中,非常好吃!");
}
@Override
public String toString() {
String str = "杨梅的信息:果实为" + this.getShape() + "、" + this.getColor()
+ "," + this.getTaste() + ",非常好吃!";
return str;
}
}
package fruits;
public class Banana extends Fruits {
private String variety;
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public Banana() {
super();
}
public Banana(String shape, String taste, String variety) {
super(shape, taste);
this.setVariety(variety);
}
public void advantage() {
System.out.println(this.getVariety() + "过形" + this.getShape() + ",过肉"
+ this.getTaste() + ",可供审食。");
}
public void advantage(String color) {
System.out.println(this.getVariety() + "颜色为" + color);
}
}
package test;
import fruits.Banana;
import fruits.Fruits;
import fruits.Waxberry;
public class Test {
public static void main(String[] args) {
Fruits fru1 = new Fruits("圆形", "酸甜适中");
Fruits fru2 = new Fruits("圆形", "酸甜适中");
fru1.eat();
System.out.println("fru1和fru2的引用比较:" + fru1.equals(fru2));
System.out.println("————————————————————————————————————");
Waxberry wb = new Waxberry("圆形", "酸甜适中", "紫红色");
wb.face();
wb.eat();
System.out.println(wb);
System.out.println("————————————————————————————————————");
Banana banana = new Banana("短而稍圆", "香甜", "仙人蕉");
banana.advantage();
banana.advantage("黄色");
}
}
04-08
762
06-08
3154
05-09
1223
03-14
995
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交