这个编程练习有些疑问

来源:2-9 编程练习

一娆Hacker

2020-12-02 10:56:21

1、之前还没讲过final呢 这里面就让写一个不允许重写的方法

2、

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

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

这样赋值对吗

不懂多了一个color参数 为什么还要去调用父亲的方法赋值,直接全在这个子类的有参构造方法赋值不行吗


接下来是代码请老师给出修改意见 感觉写的不是很好


Test.java

public class Test {
public static void main(String[] args) {
// 实例化2个父类对象,传入两组相同的参数值
Fruits fru1=new Fruits("圆","甜");
Fruits fru2=new Fruits("圆","甜");

// 调用父类eat方法
fru1.eat();

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

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

// 调用子类face方法和eat方法
wax1.face();
wax1.eat();

// 测试重写toString方法,输出子类对象的信息
System.out.println("杨梅的信息:"+wax1.toString());

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

// 调用子类的advantage和它的重载方法
ban1.advantage();
ban1.advantage("黄色");

}
}

Fruits.java

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 void setShape(String shape){
this.shape=shape;
}
public String getShape(){
return shape;
}
public void setTaste(String taste){
this.taste=taste;
}
public String getTaste(){
return taste;
}


// 创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
public void eat(){
System.out.println("水果可供人们食用!");
}



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


}

Waxberry.java

//要求Waxberry类不允许有子类
public class Waxberry extends Fruits {
// 私有属性:颜色(color)

private String color;
//创建构造方法,完成调用父类的构造方法,完成属性赋值
public Waxberry(){

}
public void setColor(String color){
this.color=color;
}
public String getColor(){
return color;
}
public Waxberry(String shape,String taste,String color){
super(shape,taste);
this.setColor(color);
}

//创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中
public final void face(){
System.out.println("杨梅:"+this.getColor()+"、"+this.getShape()+",果味"+this.getTaste());
}

//重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
public void eat(){
System.out.println("杨梅酸甜适中,非常好吃");
}

//重写toString方法,输出的表现形式不同(输出shape,color,taste)
public String toString(){
return "果实为"+this.getShape()+"、"+this.getColor()+","+this.getTaste()+"非常好吃!";
}
}

 Banana.java

public class Banana extends Fruits {
// 私有属性:品种(variety)
private String variety;

//创建带参构造方法为所有属性赋值
public Banana(String shape,String taste,String variety){
this.setShape(shape);
this.setTaste(taste);
this.setVariety(variety);
}
public void setVariety(String variety){
this.variety=variety;
}
public String getVariety(){
return variety;
}

//创建无参无返回值的advantage方法,描述为:**果形**,果肉香甜,可供生食。
public void advantage(){
System.out.println(this.getVariety()+"果形"+this.getShape()+",果肉香甜,可供生食。");
}

//创建重载advantage方法(带参数color),描述为:**颜色为**
public void advantage(String color){
System.out.println(this.getVariety()+"颜色为"+color);
}

}



写回答

1回答

好帮手慕阿园

2020-12-02

同学你好

1、是的,本章练习题涉及到了final关键字的使用,但也是稍微涉及了一点,这里同学可以先了解下;final是最终的意思,所以使用final修饰的类与方法是最终的类与方法,实现不允许有子类,与不允许重写的方法。

2、是可以的,但是这里是想让同学练习下在子类中调用父类构造方法,完成赋值

3、同学的代码整体完成的不错,只不过题目要求Waxberry类不允许有子类,可以使用final关键字进行修饰;其余代码完成的很好,继续加油

祝学习愉快

0

0 学习 · 16556 问题

查看课程