[英]Override doesn't seem to work
我有點困惑,我認為這應該可行。 它只是父類和子類,我無法弄清楚為什么a)eclipse抱怨,b)實例化對象中未調用重寫的方法。
public class Selector {
private Node rootNode;
private Grid childGrid;
public Selector(){
super();
}
public Selector(Grid childGrid){
this();
this.childGrid = childGrid;
}
public Selector(Node rootNode,Grid childGrid){
this();
this.rootNode = rootNode;
this.childGrid = childGrid;
}
private ArrayList<ArrayList<String>> filter(ArrayList<String> keys){
return null;
}
private ArrayList<ArrayList<String>> innerEneryOrder(ArrayList<ArrayList<String>> children){
return children;
}
private ArrayList<ArrayList<String>> outerEneryOrder(ArrayList<ArrayList<String>> children){
return children;
}}
好的,這是派生類:
public class StandardSelector extends Selector {
@Override
private ArrayList<ArrayList<String>> filter(ArrayList<String> keys){
ArrayList<ArrayList<String>> ret = new ArrayList<>();
for (String s: keys){
ArrayList<String> aL = new ArrayList<String>();
aL.add(s);
ret.add(aL);
}
return ret;
}}
那么,問題出在哪里呢?
增加filter
方法的可見性,以便可以覆蓋它。
私有類成員或構造函數只能在包含該成員或構造函數聲明的頂級類(第7.6節)的主體內訪問。
因此,請更換
private ArrayList<ArrayList<String>> filter(ArrayList<String> keys){
與
protected List<List<String>> filter(List<String> keys) {
在Super類中將filter()
方法的可見性更改為protected
,以便可以覆蓋它。 私有方法不能被覆蓋。 在您的情況下,您剛剛為子類創建了一個新方法,它與基類filter()
方法無關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.