它是一个接口,在android的文档中没有错误。你也可以在SharedPreferences的源代码中看到:
public interface SharedPreferences {
挖掘android的源码,我们可以看到Activity扩展自ContextWrapper
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback {
查看ContextWrapper.java,它从Context类调用getSharedPreferences函数
Context mBase;
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
return mBase.getSharedPreferences(name, mode);
}
在Context.java中声明为abstract函数,
/**
* Interface to global information about an application environment. This is
* an abstract class whose implementation is provided by
* the Android system. It
* allows access to application-specific resources and classes, as well as
* up-calls for application-level operations such as launching activities,
* broadcasting and receiving intents, etc.
*/
public abstract class Context {
public abstract SharedPreferences getSharedPreferences(String name, int mode);
}
总而言之,SharedPreferences 在每个 Context 实现上都在 class(如每个 interface)中实现。如果我们看一下Context 的源代码中的 cmets,我们可以看到:
这是一个抽象类,其实现由
安卓系统
如果您想了解有关Context 的更多信息,请点击此处了解更多信息:What is Context in Android?