Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中修改方法参数或返回_Java - Fatal编程技术网

在java中修改方法参数或返回

在java中修改方法参数或返回,java,Java,我在android源代码中见过这种方法 protected LocaleConfiguration doInBackground(Void... unused) { LocaleConfiguration localeConfiguration = new LocaleConfiguration(); readConfiguration(Launcher.this, localeConfiguration);

我在android源代码中见过这种方法

 protected LocaleConfiguration doInBackground(Void... unused) {
                    LocaleConfiguration localeConfiguration = new LocaleConfiguration();
                    readConfiguration(Launcher.this, localeConfiguration);
                    return localeConfiguration;
                }
 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
        DataInputStream in = null;
        try {
            in = new DataInputStream(context.openFileInput(PREFERENCES));
            configuration.locale = in.readUTF();
            configuration.mcc = in.readInt();
            configuration.mnc = in.readInt();
        } catch (FileNotFoundException e) {
            // Ignore
        } catch (IOException e) {
            // Ignore
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }
    }
为什么不像这样

 private static LocaleConfiguration readConfiguration(Context context) {
        LocaleConfiguration configuration = new LocaleConfiguration();
        DataInputStream in = null;
        try {
            in = new DataInputStream(context.openFileInput(PREFERENCES));
            configuration.locale = in.readUTF();
            configuration.mcc = in.readInt();
            configuration.mnc = in.readInt();
        } catch (FileNotFoundException e) {
            // Ignore
        } catch (IOException e) {
            // Ignore
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }
        return configuration;
    }

修改参数而不是返回新值的好处是什么

修改参数而不是返回新实例的好处是将实例化的控制权交给调用代码,即允许它重用现有实例

“修改参数”方法允许您使用几种这样的方法初始化对象。e、 g

LocaleConfiguration localeConfiguration = new LocaleConfiguration();
readConfiguration(Launcher.this, localeConfiguration);
readSomeOtherConfiguration(Launcher.this, localeConfiguration);
return localeConfiguration;
可以说,您可以通过返回作为参数传入的相同实例来做同样的事情,但我个人认为这是自找麻烦

另一个可能的原因可能是,如果实例化的成本很高,您可能希望回收旧对象。不过,您展示的代码似乎并非如此,这是一种优化,因此只有在绝对必要时才考虑这样做

就我个人而言,我倾向于采用“返回新实例”的方法,除非有特定的理由不这样做。我认为它更简单,并且减少了调用代码中出现细微错误的可能性