package?guoke;
public?class?Bus?extends?Vehicle?{?//Bus類繼承?Vehicle類。在Bus.java我還建了其他Vehicle的子類,car,bicycle類
/*問:為什么通過反復(fù)調(diào)用不同子類的date方法,
name,number,way屬性只賦值一次????我是這樣調(diào)用的???Bus?bus=new?Bus();??
???????????????????????????????????????????????????bus.date();
???????????????????????????????????????????????????Car?car=new?Car();
???????????????????????????????????????????????????car.date();
?結(jié)果顯示全是第一次調(diào)用的結(jié)果,比如上面這樣寫的話,?就是顯示兩次bus的屬性。
??????????????????????????????????????????????????
*/
/*?父類這樣寫的
?????public?class?Vehicle?{
?????protected??String?name;
?????protected??int?number;
?????protected??String?way;
?????public??void?date(){
???? System.out.println("交通工具可以載客");
?????}
}
*/??
?????protected??String?name="bus";//屬性
?????protected??int?number=40;
?????protected??String?way="land";
?????protected??String?name="bus";
?????protected??int?number=40;
?????protected??String?way="land";
public?void?date()?{//顯示屬性的值
//?TODO?Auto-generated?constructor?stub
System.out.println("公交汽車可以載客:");
???System.out.println(name);
???System.out.println("載客人數(shù)number:"+number);
???System.out.println(way);
}
??
}
class?Car?extends?Vehicle?{?????????//car子類
protected??String?name="car";
????protected??int?number=4;
????protected??String?way="land";
public?void?date()?{
//?TODO?Auto-generated?constructor?stub
System.out.println("轎車可以載客:");
???System.out.println(name);
???System.out.println("載客人數(shù)number:"+number);
???System.out.println(way);
}
class?Bicycle?extends?Vehicle?{????//Bicycle子類
protected??String?name="bicycle";
???protected??int?number=2;
???protected??String?way="land";
public?void?date()?{
//?TODO?Auto-generated?constructor?stub
System.out.println("自行車可以載客:");
???System.out.println(name);
???System.out.println("載客人數(shù)number:"+number);
???System.out.println(way);
?
}
請問為什么反復(fù)調(diào)用不同子類的方法,屬性的值不會變,只賦值一次?
風(fēng)中過客
2015-04-16 18:37:01