“超级"问题:“错误:"(“预期") [英] "Super" issue: "error: '(' expected"
问题描述
您好,感谢阅读!
我使用for"来读取数组列表中的每个对象,并想检查为每个对象返回函数 getType() 的字符串,该字符串存在于对象的扩展类中.
I use a "for" to read every object in an arraylist, and want to check the string that returns the function getType() for each object, which exists in the extended class of the objects.
for(int i=0; i<cat.items.size(); i++)
{
if ( cat.items.get(i).super.getType().equals(type) );
{
但是,我收到错误
MainApp.java:17: error: '(' expected
if ( cat.items.get(i).super.getType().equals(type) );
^
我认为我没有正确使用超级".你能提供一个替代方案吗?
I think that I don't user "super" correctly. Can you offer an alternative?
再次感谢!
推荐答案
你能提供替代方案吗?
Can you offer an alternative?
假设getType
是由类或子类实现的,那么使用方法如下:
Assuming that getType
is implemented by the class or a suberclass, then this is the way to use it:
cat.items.get(i).getType().equals(type)
请注意,如果子类覆盖了在超类中实现的 getType()
方法,则上面将调用该方法的子类版本.不能在超类1中调用被覆盖的方法.
Note that if the subclass overrides a getType()
method that is implemented in the superclass, the above will call the subclass version of the method. You can't call the overridden method in the superclass1.
1 - 嗯……不是在这种情况下.覆盖方法可以调用super.getType()
,但这只能对目标对象进行;即这个".它不适用于您的示例.
1 - Well ... not in this case. An override method can call super.getType()
, but that can only be done for the target object; i.e. "this". It doesn't apply to your example.
这篇关于“超级"问题:“错误:"(“预期")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!