java基础多态问题
class Car{private String color = "red";
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
}
class Benz extends Car{
private String color = "black";
}
class Test{
public static void main(String[] args){
Benz b1 = new Benz();
System.out.println(b1.getColor());
Benz b2 = new Benz();
b2.setColor("yellow");
System.out.println(b2.getColor());
}
}
输出结果第一个是red,第二个是yellow为什么呢?