4-3编程练习检查

来源:4-3 编程练习

aming41

2020-05-22 21:56:16

public class Test {
    public static void main(String[] args) {
		// 实例化2个父类对象,传入两组相同的参数值
		Fruits f1 = new Fruits("奇异果", "绿色");
		Fruits f2 = new Fruits("奇异果", "绿色");

		// 调用父类eat方法
		System.out.println(f1.eat());

		// 测试重写equals方法,判断两个对象是否相等
	    System.out.println("f1和f2的引用比较" + f1.equals(f2));

	System.out.println("————————————————————————————————————————");
		// 实例化子类对象,并传入相关参数值
	    Waxberry w = new Waxberry("圆形", "果味酸甜适中", "紫红色");

		// 调用子类face方法和eat方法
	    System.out.println(w.face());
        System.out.println(w.eat());
		// 测试重写toString方法,输出子类对象的信息
		System.out.println(w.toString());

		System.out.println("——————————————————————————————————————————————");
		// 实例化Banana类对象,并传入相关参数值
		Banana b = new Banana("短而稍圆", "香甜", "仙人蕉", "黄色");

		// 调用子类的advantage和它的重载方法
	    b.advantage();
        b.advantage("黄色");
	}
}
public class Fruits {
    // 私有属性:水果的形状(shape)和口感(taste)
	private String shape;
	private String taste;

	public Fruits() {

	}

	// 带参构造函数(参数为shape和taste)
	public Fruits(String shape, String taste){
	    this.setShape(shape);
	    this.setTaste(taste);
	}


    //通过封装实现对私有属性的get/set访问
   public String getShape(){
       return this.shape;
   }
   
   public void setShape(String shape){
       this.shape = shape;
   }
   
   public String getTaste(){
       return this.taste;
   }
   
   public void setTaste(String taste){
       this.taste = taste;
   }
	// 创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
	public String eat(){
	    String str = "水果可供人们食用!";
	    return str;
	}



	// 重写equals方法,比较两个对象是否相等(比较shape,taste)
	public boolean equals(Fruits f){
	    if(f == null){
	        return false;
	    }
	    
	    if(this.getShape().equals(f.getShape()) && (this.getTaste().equals(f.getTaste()))){
	        return true;
	    }else{
	        return false;
	    }
	}


}
public class Banana extends Fruits{
    // 私有属性:品种(variety)
    private String variety;
    private String color;
    public Banana(){
        
    }
    
    //创建带参构造方法为所有属性赋值
    public Banana(String shape, String taste, String variety, String color){
        super(shape, taste);
        this.setVariety(variety);
        this.setColor(color);
    }
    
    public String getVariety(){
        return this.variety;
    }
    
    public void setVariety(String variety){
        this.variety = variety;
    }
    
        public String getColor(){
        return this.color;
    }
    
    public void setColor(String color){
        this.color = color;
    }
    
    //创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
    public void advantage(){
        System.out.println(this.getVariety() + "果形" + this.getShape() + ", 果肉" + this.getTaste() + ", 可供生食。");
    }
    
    //创建重载advantage方法(带参数color),描述为:**颜色为**
    public void advantage(String color){
        System.out.println(this.getVariety() + "颜色为" + this.getColor());
    }

}
//要求Waxberry类不允许有子类
final public class Waxberry extends Fruits{
  // 私有属性:颜色(color)
  private String color;
  
  public Waxberry(){
      
  }
	//创建构造方法,完成调用父类的构造方法,完成属性赋值
  public Waxberry(String shape, String taste, String color){
      super(shape, taste);
      this.setColor(color);
  }
  
  public String getColor(){
      return this.color;
  }
  
  public void setColor(String color){
      this.color = color;
  }
  
  //创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中
  public final String face(){
      String str = "杨梅: " + this.getColor() + "、 " + this.getShape() + ", " + this.getTaste() + "。";
      return str;
  }
  
  //重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
  
  public String eat(){
      String str = "杨梅酸甜适中,非常好吃!";
      return str;
  }
  
  //重写toString方法,输出的表现形式不同(输出shape,color,taste)
  public String toString(){
      return "杨梅的信息:果实为" + this.getShape() + "、" + this.getColor() + ","+ this.getTaste() + ",非常好吃!";
  }
}


写回答

1回答

好帮手慕阿园

2020-05-23

同学你好,课题完成的不错,很棒;但是还有个地方需要优化一下

题目中的任务是创建无参无返回值的方法,同学创建的有返回值

http://img.mukewang.com/climg/5ec8c556095ae85105540131.jpg

修改如下

http://img.mukewang.com/climg/5ec8c55e094b5dc705300100.jpg

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~


0

0 学习 · 11489 问题

查看课程