有困难的时候做它利用非静态方法静态方法

问题描述:

我大约6个星期到学习Java,并仍然实现静态方法挣扎(以为我真正理解它,但是这证明我错了!)。

I'm about 6 weeks into learning Java and still struggling with implementing static methods (thought I really understood it, but this proved me wrong!).

我试图使可用本地存储的键值对的值公开。这是我最初的code:

I'm trying to make the value of a locally stored key-value pair available publicly. Here's my initial code:

public class Settings extends Activity implements OnClickListener {
        public Settings(TextView loginText, TextView passwdText) {
    super();
    this.loginText = loginText;
    this.passwdText = passwdText;
}

public static String getDriverNum() {
    SharedPreferences sp = getSharedPreferences(DELEX_SP, MODE_PRIVATE); <---ERROR
    String Login = sp.getString("KeyLgn", "No Login Found");
    return Login;
}

当然,我得到一个错误无法使静态参考非静态方法从类型ContextWrapper getShared preferences(字符串,INT),所以我尽量包住非静态方法在我自己的公共方法,作为一个类似计算器answer说明:

public class Settings extends Activity implements OnClickListener {
public Settings(TextView loginText, TextView passwdText) {
    super();
    this.loginText = loginText;
    this.passwdText = passwdText;
}

public static String getDriverNum() {
    String Login = getSharedPref().getString("KeyLgn", "No Login Found"); <-- SAME ERROR
    return Login;
}

public  SharedPreferences getSharedPref() {
    SharedPreferences sp = getSharedPreferences(DELEX_SP, MODE_PRIVATE);
    return sp;
}

但是,这只是造成了同样的错误,因为我还没有解决,非静态调用的 getShared preferences 的方法。什么是解决这个的最佳方式?难道它来创建一个包装的 getShared preferences 的,而不是一类?

But this just caused the same error as I haven't resolved the call to the non-static getSharedPreferences method. What's the best way to resolve this? Is it to create a class that wraps getSharedPreferences instead?

感谢您的耐心,而我与静态命名奋斗。

Thanks for your patience while I struggle with static nomenclature.

如果你想写,利用非静态方法静态方法,你只是传递一个实例,它是这样的:

If you want to write a static method that utilizes non-static methods, you just pass an instance to it like this:

public static void invokeMethod(SomeObject foo) {
    foo.bar();
}

所以,你在做什么是一个伟大的格局。我用它所有的时间,可以在多个类别中重复使用(又名组成)助手只是让你SomeObject背景下的。

So what you're doing is a great pattern. I use it all the time for "helpers" that can be reused across many classes (aka composition) Just make your "SomeObject" the Context.

下面是我在Android中使用的模式来得到一个不错的中心点定义默认preferences:

Here's the pattern I use in Android to get a nice central point to define default preferences:

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.HashMap;
/**
 * Provides support functions for getting preferences as well as a
 * central place for storing default preferences.
 */
public final class PreferencesHelper {
    public static class Preferences {
        public static final String SOME_SETTING = "SOME_SETTING";
    }

   /** This allows us to define our default preferences all in one place */
    private static HashMap<String, Object> sDefaultPreferences =
        new HashMap<String, Object>(){{
        put(Preferences.SOME_SETTING, "value");
    }};

    public static SharedPreferences getDefaultPreferences(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static String getString(SharedPreferences prefs, String key) {
        return prefs.getString(key, (String) sDefaultPreferences.get(key));
    }

    /* Convenience method (use when getting only one preference) */
    public static String getString(Context context, String scanner) {
        SharedPreferences prefs = getDefaultPreferences(context);
        return getString(prefs, scanner);
    }

...

该模式允许默认值的定义是在一个地方。

This pattern allows the definition of default values to be in one place.