动态绑定数组(Dynamic Binding arrays)

系统教程 行业动态 更新时间:2024-06-14 16:59:17
动态绑定数组(Dynamic Binding arrays)

我的程序中的动态绑定功能有问题。

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.

更多推荐

本文发布于:2023-04-16 14:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/c36fd8f68113df241ae315279e7ab2e8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   绑定   动态   arrays   Binding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!