【Java继承】编程练习:水果的信息描述

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);
     }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值