保护不可见的子​​领域 [英] Protected fields not visible to subclasses

查看:135
本文介绍了保护不可见的子​​领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这直接延伸 android.view.View 自定义视图。如果我尝试访问域 mScrollX mScrollY ,我看到一个错误,该字段无法得到解决或不一个场。该<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/view/View.java;h=65904972d39662613c67f549a26a0213b77747cc;hb=refs/heads/gingerbread-release">source $ C $下android.view.View 有mScrollX,mScrollY,并宣布类似的变量保护。它怎么说,我直接的子类不能访问它的父类的受保护的领域? (类如<一个href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/widget/ScrollView.java;h=2ad67baa4d423b227123b019e6da44101018269c;hb=refs/heads/gingerbread-release">ScrollView显然是可以的。)

I'm writing a custom view that directly extends android.view.View. If I try to access fields mScrollX or mScrollY, I see an error that the field "cannot be resolved or is not a field." The source code for android.view.View has mScrollX, mScrollY, and similar variables declared protected. How is it that my direct subclass cannot access protected fields of its parent class? (Classes like ScrollView apparently can.)

P.S。我意识到,我可以叫 getScrollX(),但我想更新这些字段;调用 setScroll()有副作用,我不想。

P.S. I realize that I can call getScrollX(), but I want to update these fields; calling setScroll() has side effects that I don't want.

推荐答案

这是因为他们不是Android SDK的一部分。

It's because they are not part of the Android SDK.

下面是源$ C ​​$下 mScrollX

Here is the source code for mScrollX:

/**
 * The offset, in pixels, by which the content of this view is scrolled
 * horizontally.
 * {@hide}
 */
@ViewDebug.ExportedProperty(category = "scrolling")
protected int mScrollX;

您会注意到 @hide 注释。这意味着,这不是Android SDK的一部分。构建过程中,创造了Android SDK的一部分,将不包括在存根编辑该数据成员 android.view.View 是在的android.jar 您对编译文件。

You will notice the @hide annotation. That means this is not part of the Android SDK. The part of the build process that creates the Android SDK will not include this data member in the stub edition of android.view.View that is in the android.jar file that you are compiling against.

@hide 标注可用于改变,对于以公共需要的内部目的或保护,但不被视为东西SDK开发人员应该使用。

The @hide annotation is used for things that for internal purposes needed to be public or protected but are not considered something SDK developers should be using.

请查找其他解决方案的任何问题,您遇到。

Please find other solutions for whatever problem you are experiencing.

这篇关于保护不可见的子​​领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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