我有一个对象数组,其中一些使用扩展版本,其中包含基类中不可用的函数。当数组是由基类定义时,如何通过数组调用该函数?
Example
Shape[] shapes = new Shape[10];
shapes[0] = new Circle(10) //10 == radius, only exists in circle class which extends Shape
shapes[0].getRadius(); //Gives me a compilation error as getRadius() doesn't exist in the
Shape class, only in the extended Circle class. Is there a way around this?
Shape
类不包含方法getRadius
因此无需投射对象Shape
to Circle
,该方法将不可见。所以你应该使用这个:
((Circle)shapes[0]).getRadius();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)