父类。
public class ZuoYe{
private String shape;//形状
private String taste;//口感
public ZuoYe() {
}
public ZuoYe(String shape,String taste) {
this.shape=shape;
this.taste=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(Object obj) {
if(obj==null)
return false;
ZuoYe temp=(ZuoYe)obj;
if(this.getShape().equals(temp.getShape())&&this.getTaste().equals(temp.getTaste()))
return true;
else
return false;
}
子类。
public class ZuoYeTest extends ZuoYe{
private String color;
public ZuoYeTest() {
}
public ZuoYeTest(String shape,String taste,String color ) {
super(shape,taste);
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String face(String color) {
this.color=color;
System.out.println("杨梅:"+color+"、"+this.getShape()+",果味酸甜适中");
return color;
}
public void eat() {
System.out.println("杨梅酸甜适中,非常好吃!");
}
public String toString() {
return "杨梅的果实信息:果实为"+this.getShape()+"、"+this.color+this.getTaste();
}
}
测试类。
public class Test {
public static void main(String[] args) {
ZuoYe fru1=new ZuoYe("圆形","香");
ZuoYe fru2=new ZuoYe("圆形","香");
fru1.eat();
boolean flag=fru1.equals(fru2);
System.out.println("fru1和fru2的引用比较:"+flag);
System.out.println("=============================");
ZuoYeTest zy=new ZuoYeTest();
zy.face("紫红色");
zy.setShape("圆形");
zy.setTaste("酸甜适中,非常可口");
zy.eat();
System.out.println(zy);
}
}
输出结果
杨梅:紫红色、null,果味酸甜适中
getShape为啥到最后返回的空值啊 |
|
|
|
评论
直达楼层