我的程序中的动态绑定功能有问题。
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();的错误lines b[1].get_bedrooms(); 和b[1].get_bathrooms(); 它找不到符号get_bathrooms和get_bedrooms。 我在子类House有这些函数,并将它分配给数组中的[1]插槽。 为什么它不在子类中注册该函数? 感谢您的帮助,也许没有解释自己最好的,我在这里新...
Im having trouble with the dynamic binding feature in my program.
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();I get the error for lines b[1].get_bedrooms(); and b[1].get_bathrooms(); that it cannot find the symbol get_bathrooms and get_bedrooms. I have these functions in the subclass House and have assigned it to the [1] slot in the array. Why does it not register the function in the subclass? Thanks for the help and maybe not explaining myself the best, Im new here...
最满意答案
如果使用多态数组,则必须对这些值进行类型转换。
((House)b[1]).get_bedrooms();当你最初听到它时,多态性听起来是一个非常有用的功能,它非常有用,但不是很有用。
You have to typecast those values if you are using a polymorphic array.
((House)b[1]).get_bedrooms();Polymorphism sounds like a very useful feature when you heard it at first, and it is very useful, but not as useful.
更多推荐
发布评论