【问题标题】:Using variables from another method - problems understanding the object orientation使用其他方法的变量 - 理解面向对象的问题
【发布时间】:2015-10-30 13:53:34
【问题描述】:

以下代码不完整,但我的问题主要集中在方法 processConfig() 上。它从文件中读取属性,我想将这些属性移交给方法 replaceID()。当 processConfig 的内容在 main() 方法中时,它已经工作了。但现在我想把这段代码放到它自己的方法中。移交属性的最佳方式是什么(我保存在字符串中,如 difFilePath)。我对 OO 编程不太熟悉,想理解这个概念。感谢您的帮助。

public class IDUpdater {

....

public static void main(String[] args) throws Exception {

    //Here I want to call the variables from processConfig() to make them available for replaceID(...)

    replaceID(difFilePath, outputDifPath, encoding);

}

public static void replaceID(String difFilePath, String outputDifPath, String encoding) throws Exception{


    return record;
}

public void processConfig(){

    Properties prop = new Properties();
    InputStream input = null;
    try {
        input = new FileInputStream("config.properties");
    } catch (Exception e) {
        logger.error("File 'config.properties' could not be found.");
    }
    try {
        prop.load(input);
    } catch (Exception e) {

        logger.error("Properties file could not be loaded.");
    }

    String difFilePath = prop.getProperty("dif_file_path");
    String outputDifPath = prop.getProperty("output_dif_path");
    String encoding = prop.getProperty("encoding");

}

}

【问题讨论】:

    标签: java object methods


    【解决方案1】:

    您必须全局声明变量。这样就可以在每种方法中访问它们。在全局声明它们后,首先在 main 方法中调用 processConfig,这会将变量设置为应有的值。

    public class IDUpdater {
        private String difFilePath;
        private String outputDifPath;
        private String encoding;
    
        public void main(String[] args) throws Exception {
            processConfig();
            replaceID(); 
        }
    
        public void replaceID() throws Exception{
            // You can use your variables here.
            return record;
        }
    
        public void processConfig(){
            Properties prop = new Properties();
            InputStream input = null;
    
            try {
                input = new FileInputStream("config.properties");
            } catch (Exception e) {
                logger.error("File 'config.properties' could not be found.");
            }
    
            try {
                prop.load(input);
            } catch (Exception e) {
                logger.error("Properties file could not be loaded.");
            }
    
            difFilePath = prop.getProperty("dif_file_path");
            outputDifPath = prop.getProperty("output_dif_path");
            encoding = prop.getProperty("encoding");  
        }
    }
    

    请注意,我私下声明了变量。有关保护变量的更多信息,请参阅https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

    【讨论】:

    • 我会三思而后行使用“全局声明变量”短语。您可能需要改写它,否则您可能会面临被扔石头的可能性。
    • 但是我遇到了“静态”问题。当我将字符串设为静态时,我之后无法更改它们。我已经试过了。不过谢谢你的建议。编辑:当我调用 replaceID(difFilePath, outputDifPath, encoding);它说“无法对非静态字段 diffFilePath 进行静态引用”
    • @Kai 我不确定你所说的静态问题是什么意思?
    • 当我在你的建议中尝试它时,Eclipse 在“replaceID(difFilePath, outputDifPath, encoding);”行告诉我它“无法对非静态字段 diffFilePath 进行静态引用”。如果我把它们变成静态的,我就无法改变它们..
    • @Kai 你的 replaceID 方法必须是静态的吗?
    【解决方案2】:

    您可能想阅读有关封装和对象主题的文章(或者更好的是一本书)。 Thisthis 对您来说可能是一个很好的起点。如果您不了解代码背后的概念,那么任何人修复您的代码都是没有意义的。

    【讨论】:

    • 谢谢。我不希望任何人修复我的代码。我只是想弄清楚我的问题是什么。我会读那篇文章。我已经阅读了其他一些关于它的文章,但我仍然觉得我还没有完全理解它。
    猜你喜欢
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多