1
我的程序中動態綁定功能有問題。動態綁定數組
Building[] b = new Building[3];//creates the object b
b[0] = new Building(squarefootage, stories);
b[1] = new House(squarefootage, stories, beds, baths);
b[2] = new School(squarefootage, stories, classes);
b[0].get_squarefootage();//calls the user to enter the area
b[0].get_stories();//calls the user to enter the floors
b[1].get_bedrooms();
b[1].get_bathrooms();
我得到lines b[1].get_bedrooms();
和b[1].get_bathrooms();
錯誤,它無法找到符號get_bathrooms和get_bedrooms。我在子類House
中擁有這些函數,並將它們分配給數組中的[1]插槽。爲什麼它不在子類中註冊函數?感謝您的幫助,也許不是最好的解釋我自己,我在這裏新...
這是因爲你的數組是'Building',因此'b [i]'是'Building' - 而不是你放在那裏的任何子類。如果你想要「家」或「學校」,你必須演員。編譯器如何知道'b [1]'是'House'?如果我要做'b [1] = getABuildingFromSomewhere()'怎麼辦? –