哪个类在 Android 中实现 ContextWrapper 的方法?
当我开发 Android 应用程序时,我喜欢查看内部 SDK 实现。这是一个巨大的框架,有时如果您知道内部方法是如何实现的,它会很有帮助。
现在我所做的最后一次源代码检查确实让我感到困惑。我查看了 Context 类来阅读一些实现。不幸的是,它们中的大多数都是抽象的,这就是 Android 发明 ContextWrapper(一种简单的适配器模式)的原因。
问题是,我找不到一些我感兴趣的方法。让我们以 getResources
为例。我有一个 ContextObject 并对其调用 getResources 。此 Context 是 Activity
的一个实例(它本身没有实现 getResources()
)。
ContextThemeWrapper 也是如此,它是 Activity 的直接父类。然后,ContextWrapper 在其 Context
成员上调用 getResources()
。但谁来实施呢?
编辑:从 ContextWrapper 添加了片段,
public class ContextWrapper{
Context mContext
public ContextWrapper(Context ctx){
mContext = ctx
}
public Resourced getResources(){
return mContext.getResources()
}
//... all other Methods are implementing the same AdapterPattern
}
因此问题也可以是“哪个 Context 传递给 ContextWrapper,它正在实现所需的方法”
While I'm developing Android-Apps I like to have a look at internal SDK implementations. It's a huge Framework and sometimes it helps a lot if you know, how internal Methods are implemented.
Now the last source-check I have done is really confusing to me. I looked at Context
class to read some implementations. Unfortunately most of them are abstract, that's why Android invents the ContextWrapper, (a simple Adapter Pattern).
The problem is, that I couldn't find some Methods I'm interested in. Lets take getResources
as Example. I have a ContextObject and call getResources on it. This Context is an instance of Activity
(which is not implementing getResources()
by itself).
Same for ContextThemeWrapper which is the direct parent-class of Activity. The ContextWrapper then invokes getResources()
on its Context
member. But who is implementing it then?
EDIT: added Snippet from ContextWrapper
public class ContextWrapper{
Context mContext
public ContextWrapper(Context ctx){
mContext = ctx
}
public Resourced getResources(){
return mContext.getResources()
}
//... all other Methods are implementing the same AdapterPattern
}
so the question can also be "which Context is passed to ContextWrapper, which is implementing required methods"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于通过 nicholas.hausschild 的有用提示自己找到了答案,只需运行调试器即可。
不幸的是,这似乎是找出这一点的唯一方法,因为它是在一个名为 ContextImpl 的类中实现的,该类是包可读的(尽管没有记录)。您必须安装 Eclipse 源代码插件(这似乎有点问题)安装(如果您已下载 Android-SDK 并将其链接到 eclipse)以查看源代码。
对于对这些来源感兴趣的任何人: 点击我
I finally found it out myself with the helpful tip from nicholas.hausschild, to just to run the debugger.
Unfortunatly this seems to be the only way, to find that out, because it's implemented in a Class called
ContextImpl
, which is package-readable (though not documented). You have to install Eclipse source-code plugin (which seems to be a little buggy to install, if you had downloaded and linked the Android-SDK to eclipse already) to see the sources.For anyone who is interested in these sources: clickme
Context
类上的getResources()
方法是抽象的,这意味着从它继承的任何内容都需要提供或重写其实现。ContextWrapper
类上的getResources()
实现是具体的,您应该在其中找到其代码。另外,你的继承链有点倒退。
Activity
是ContextThemeWrapper
的子类,而不是相反:Android 开发人员参考 将帮助您了解继承结构。
The
getResources()
method on theContext
class is abstract, meaning whatever inherits from it needs to provide or override its implementation. The implementation ofgetResources()
on theContextWrapper
class is concrete, and is where you should find its code.Also, you have your inheritance chain a little backwards.
Activity
is a subclass ofContextThemeWrapper
, not the other way around:The Android Developer Reference will help you with the inheritance structures.