2-9编程作业
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类不允许有子类
public class Waxberry extends Fruits{
// 私有属性:颜色(color)
private String color;
//创建构造方法,完成调用父类的构造方法,完成属性赋值
//有参构造调用有参构造
public Waxberry(String color){
super("圆形","酸甜适中");
this.setColor(color);
}
//有参构造调用有参构造2
// public Waxberry(String shape,String taste,String color){
// super(shape,taste);
// this.setColor(color);
// }
public void setColor(String color){
this.color=color;
}
public String getColor(){
return color;
}
//创建不允许重写的face方法,描述为:杨梅:**、**,果味酸甜适中
public 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()+",非常好吃!";
}
}
public class Banana extends Fruits {
// 私有属性:品种(variety)
private String variety;
//创建带参构造方法为所有属性赋值
public Banana(String variety){
super("短而稍圆","果肉香甜");
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);
}
}
public class Test {
public static void main(String[] args) {
// 实例化2个父类对象,传入两组相同的参数值
Fruits one=new Fruits("苹果","甜");
Fruits two=new Fruits("苹果","甜");
// 调用父类eat方法
one.eat();
// 测试重写equals方法,判断两个对象是否相等
// boolean flag=one.equals(two);
System.out.println("fru1和fru2的引用比较:"+one.equals(two));
System.out.println("————————————————————————————————————————");
// 实例化子类对象,并传入相关参数值
Waxberry wax=new Waxberry("紫红色");
// Waxberry wax=new Waxberry("圆形","酸甜适中","紫红色");
// 调用子类face方法和eat方法
wax.face();
wax.eat();
// 测试重写toString方法,输出子类对象的信息
System.out.println(wax.toString());
System.out.println("——————————————————————————————————————————————");
// 实例化Banana类对象,并传入相关参数值
Banana ba=new Banana("仙人蕉");
// 调用子类的advantage和它的重载方法
ba.advantage();
ba.advantage("黄色");
}
}
问题1://要求Waxberry类不允许有子类
是说在Wax类中定义的有color 这个变量,
banana这个类中也要有颜色,不能去捆绑两者间吗?还是什么意思?题干这句话没看懂
问题2:
banana类中 最后方法中的参数有color,
输出时必须
public void advantage(String color){
System.out.println(this.getVariety()+"颜色为"+color);
如果想引用this.getColor()方法的话, 必须是无参方法才可以吧,同时需要引用包下的类,(import)也就是
public void advantage(){
System.out.println(this.getVariety()+"颜色为"+this.getColor()); 这样的话就是方法重写了吧。不是重载了。
问题3 : Fruits 方法中,equals重写的时候
public boolean equals(Object obj) 这个obj也就是最后定义的temp,我明白
那么 谁传进来了? 成为了这个obj也就是temp呢?
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 9410 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程









恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星