2015-05-02 41 views
0

您好,感謝閱讀「超級」的問題:「錯誤:‘(’預期」!

我用「爲」讀取一個ArrayList中的每個對象,並要檢查返回函數的字符串的getType()爲每個對象,它存在於擴展的類的對象。

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)); 
         ^

我認爲我沒有用戶的「超級」正確。你能否提供替代品?

再次感謝!

回答

0

Can you offer an alternative?

假設getType由類或suberclass實現,那麼這是使用它的方式:

cat.items.get(i).getType().equals(type) 

注意,如果子類覆蓋一個getType()方法是在超類中實現,上面將調用該方法的子類版本。您不能在超類中調用重寫的方法。


1 - 呃......不是這種情況。覆蓋方法可以調用super.getType(),但只能針對目標對象完成;即「這個」。它不適用於你的例子。

+0

非常感謝,我沒有想到這麼簡單。 – AnKo

1

super是一個保留字,不能在類似的上下文中使用。意思是,它不能是一個變量名稱,方法名稱等。如果你想在這裏做的是「get item x,然後調用它的父版本getType()」,那麼這也是一個無效的使用super

+0

我明白超級的使用是不正確的,我試圖做後者。你如何建議我打電話給getType()? – AnKo

+0

'getType()'方法應該可用於任何'super'的子類。嘗試使用'cat.items.get(i).getType()。equals(type)' – MLavrentyev

0

這可以幫助你瞭解更多關於繼承和超()...

的源代碼:

$ cat Inheritance.java 
class ParentClass { 
    private String privateParentProperty; 

    public ParentClass() { 
     this.privateParentProperty = "Hi, this is ParentClass, my instance came from " + String.valueOf(this.getClass()) + "!"; 
    } 

    public String getPrivateParentProperty() { 
     return this.privateParentProperty; 
    } 
} 

class ChildClass extends ParentClass { 
    private String childProperty; 

    public ChildClass() { 
     super(); // I can explictly call the parent constructor in a child class by calling super in the first line of the child class... 
     this.childProperty = "Hi, I came from " + String.valueOf(this.getClass()) + "!"; 
    } 

    public String getChildProperty() { 
     return this.childProperty; 
    } 

    @Override 
    public String getPrivateParentProperty() { 
     String parentProperty = super.getPrivateParentProperty(); // I can override a parent method in a child class and call that method from the parent in the first line of the child's class. 
     parentProperty += " I, " + String.valueOf(this.getClass()) + " am intercepting this call and adding my own message to my parent."; // Note: I am not modifying the private property from the parent, but rather appending to a new String local to this method. 
     return parentProperty; 
    } 
} 

class MainApp { 
    public static void main(final String[] args) { 
     ParentClass parentClassInstance = new ParentClass(); 
     System.out.println (parentClassInstance.getPrivateParentProperty()); 
     ChildClass childClassInstance = new ChildClass(); 
     System.out.println(childClassInstance.getChildProperty()); 
     System.out.println(((ParentClass)childClassInstance).getPrivateParentProperty()); // I can cast a child to its parent class and invoke the method... however, because the child overrode the method and the instance is the child instance, even with the cast, the method from the child will execute. 
     System.out.println(childClassInstance.getPrivateParentProperty()); // I can also call this method directly from the childwithout needing to cast, because it inherits it from the parent. 
    } 
} 

編譯和測試輸出:

$ javac Inheritance.java 
$ java MainApp 
Hi, this is ParentClass, my instance came from class ParentClass! 
Hi, I came from class ChildClass! 
Hi, this is ParentClass, my instance came from class ChildClass! I, class ChildClass am intercepting this call and adding my own message to my parent. 
Hi, this is ParentClass, my instance came from class ChildClass! I, class ChildClass am intercepting this call and adding my own message to my parent.

希望這有助於!