0

我正在尝试访问父类中的子类变量。你能建议我如何根据下面的代码片段进行操作吗?

public abstract class Base{

//some abstract methods
//one more method to parse the xml
    public final void parseXml(){
        String clName = Thread.currentThread().getStackTrace()[1].getClassName(); //child class name
        if(xmlFile_+clName){ //i am trying to access "Test.xmlFile_Test",
          //execute the if string is available
        }

    }
}
public class Test extends Base{
    public static final String xmlFile_Test = "<Hello>sample</Hello>";
    public int execute(){
        parseXml(); //This should call  base class method
    }
}

我的错误步骤在哪里..这是伪代码,可能会帮助您回答

4

1 回答 1

2

创建getXMLFile()在基类及其所有子类中调用的方法

public class Base{
    protected String getXMLFile(){
        return "BaseXML";
    }

    public void foo(){
        if(getXMLFile() ....){
            ...
        }
    }
}

public class Test{
    @Override
    protected String getXMLFile(){
        return "TestXML";
    }
}
于 2012-04-09T18:23:31.357 回答