android 哪个类提供对 SharedPreferences 接口(interface)的定义

标签 android class interface sharedpreferences

如果看SharedPreferences它清楚地表明它是 Android SDK 中的一个接口(interface)。

公共(public)接口(interface) SharedPreferences

谁能帮助我更好地理解哪个类准确地提供了SharedPreferences 函数的定义? ?

最佳答案

是接口(interface),android的文档没有写错。正如您在 SharedPreferences 中看到的那样的源代码也是:

public interface SharedPreferences {

挖掘android的源代码,我们可以看到ActivityContextWrapper

扩展
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 源代码中的注释,我们可以看到:

This is an abstract class whose implementation is provided by the Android system

如果您想了解有关 Context 的更多信息,这里有更多信息:What is Context in Android?

关于android 哪个类提供对 SharedPreferences 接口(interface)的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34248686/

相关文章:

java - 在 Java 中,将所有接口(interface)提取到一个单独的项目中的技术动机是什么?

android - 如何在 Android 中动态添加新的 android 芯片?

.net - 为什么我需要 Typescript 中的泛型

Java "Interface"类

C++ 如何将此类实例的 boost 共享指针传递给嵌套类对象?

javascript - 如何在javascript中将一个类的属性复制到另一个类,包括方法

c# - 无法为接口(interface)添加约束

java - FATAL EXCEPTION : main java. lang.NullPointerException: uriString while trying to share image to social media intent 安卓

android - 如何将 C++ 源代码解析为 obj?

android - SQLite 说没有这样的列