getResources() Android 的实现 [英] Implementation of getResources() Android

查看:22
本文介绍了getResources() Android 的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习android编程,对getResources()方法产生了疑问.我注意到,当我创建一个 Resources 对象时,我所要做的就是:
资源 res = getResources();
第一个疑问是以下为什么我必须这样做并且我不能使用java关键字new?我不应该做这样的事情:
Resources res = new Resources();
第二个疑问如下:在我的文件的顶部,我导入了 Resources 类.
导入android.content.res.Resources;
现在我读了 android api,它说 getResources() 是一个公共抽象方法,如果它是抽象的,哪个类实现它?如果它没有被声明为静态,我怎么能简单地调用它键入 getResources() ?

I have just started learning android programming and I came up with a doubt about the method getResources(). I noticed that when I create a Resources object all I have to do is:
Resources res = getResources();
The first doubt is the following why do I have to do in that way and I mustn't use the java keyword new? Shouldn't I do something like this:
Resources res = new Resources();
The second doubt is the following: at the top of my file I have imported the Resources class.
import android.content.res.Resources;
Now I read the android api and it says that getResources() is a public abstract method, if it is abstract which class implements it? how can I simply call it typing getResources() if it is not declared as static?

推荐答案

您的活动扩展类 android.app.Activity 反过来又扩展了类 android.content.Context(类层次结构的三个级别).类 Context 声明抽象方法 getResources() 这意味着您的活动子类继承了该方法,您可以从 onCreate() 方法中调用它(例如).

Your activity extends class android.app.Activity which in turn extends class android.content.Context (three levels up the class hierarchy). Class Context declares the abstract method getResources() which means that your activity subclass inherits that method and you can call it from within your onCreate() method (for example).

方法 getResources() 在类 Context 中被声明为抽象类,但它是类层次结构中的中间类之一 (android.view.ContextThemeWrapper) 提供了该方法的实现.

The method getResources() is declared as abstract in class Context but one of the intermediate classes in the class hierarchy (android.view.ContextThemeWrapper) provides an implementation for the method.

这也意味着创建 Resources 对象不是您的责任;而是由框架完成.

Also that means that creating the Resources object is not your responsibility; it is done by the framework instead.

这篇关于getResources() Android 的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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