1、编程题目
请使用面向对象“继承”的思想,实现杨梅和仙人蕉的信息描述。
程序参考运行效果如下所示:
水果可供人们食用!
fru1和fru2的引用比较:true
————————————————————————————————————————
杨梅:紫红色、圆形,果味酸甜适中!
杨梅酸甜适中,非常好吃!
杨梅的信息:果实为圆形、紫红色,酸甜适中,非常好吃!
————————————————————————————————————————
仙人蕉果形短而稍圆,果肉香甜,可供生食。
仙人蕉颜色为黄色
2、编程内容
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 fru3=new Waxberry("紫红色","圆形","酸甜适中");
System.out.println(fru3.face());
fru3.eat();
System.out.println(fru3.toString());
System.out.println("——————————————————————————————————————————————");
Banana fru4=new Banana("短而稍圆","果肉香甜","仙人蕉");
fru4.advantage();
fru4.advantage("黄色");
}
}
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(Fruits obj){
if(obj==null) {
return false;
}
if(this.getShape().equals(obj.shape)&&this.getTaste().equals(obj.taste)){
return true;
}else {
return false;
}
}
}
public final class Waxberry extends Fruits{
private String color;
public Waxberry() {
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Waxberry(String color,String shape,String taste){
super(shape,taste);
this.setColor(color);
}
public final String face(){
String str="杨梅:"+this.getColor()+"、"+this.getShape()+",果味酸甜适中!";
return str;
}
public void eat(){
System.out.println("杨梅酸甜适中,非常好吃!");
}
public String toString() {
String toString = "杨梅的信息:果实为"+this.getShape()+"、"+this.getColor()+",酸甜适中,非常好吃!";
return toString;
}
}
public class Banana extends Fruits{
// 私有属性:品种(variety)
private String variety;
public Banana(String shape,String taste,String variety) {
super(shape,taste);
this.setVariety(variety);
}
public Banana(){
}
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);
}
}