android提供定义到SharedPreferences接口 [英] android which class provide definition to SharedPreferences interface

查看:290
本文介绍了android提供定义到SharedPreferences接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果查看 SharedPreferences ,它会清楚地显示它是Android中的界面SDK。



public interface SharedPreferences

if look at SharedPreferences it clearly shows it's an interface in Android SDK.

public interface SharedPreferences

任何人都可以帮助我更好地理解哪个类提供了 SharedPreferences

Can anyone help me understand it better that which class exactly provides definition to functions of SharedPreferences?

推荐答案

这是一个接口,在android的文档中没有错误。如您在 SharedPreferences 的源代码:

It is an interface, there's no mistake in android's documentation. As you can see in SharedPreferences's source code too:

public interface SharedPreferences {

在Android的源代码中,我们可以看到 活动 延伸自 ContextWrapper

Digging in android's source code, we can see that Activity extends from ContextWrapper

public class Activity extends ContextThemeWrapper
    implements LayoutInflater.Factory2,
    Window.Callback, KeyEvent.Callback,
    OnCreateContextMenuListener, ComponentCallbacks2,
    Window.OnWindowDismissedCallback {

查看 ContextWrapper.java ,它从上下文

Context mBase;

@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
    return mBase.getSharedPreferences(name, mode);
}

被声明为抽象函数在 Context.java

/**
 * 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 上的类中实现(如每个接口 / code>实现。如果我们来看看 Context 的源代码中的注释,我们可以看到:

In conclusion SharedPreferences is implemented in a class(as every interface) on every Context implementation. If we take a look at the comments in Context's source code we can see that:


这是一个抽象类,它的实现由
Android系统提供

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

有关上下文的更多信息,这里是更多的信息: Android中的上下文是什么?

And in case that you want more information about Context, here is way more info: What is Context in Android?

这篇关于android提供定义到SharedPreferences接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆