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()'怎么办? –