android访问扩展customListAdapter的成员变量

发布于 2024-12-15 03:31:41 字数 958 浏览 0 评论 0原文

我在 Android 中面临着奇怪的 OOP 行为(IMO),也许在 Java 中...... 我有一个活动,在其中实例化了一个 CustomSimpleCursorAdapter 对象,它是一个扩展 SimpleCursorAdapter 的类。在我的自定义类中,我添加了一些成员变量。 当我创建该类的对象时,我希望能够访问新创建的对象的成员变量。我的 OOP 经验告诉我,以下内容应该足够了:

ListAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.currentWidth = 100;

注意:currentWith 变量被声明为公共...

(Eclipse)不想编译,所以我想也许是一个公共设置器 setCurrentWidth ()在自定义类中就可以解决这个问题。我将一个成员变量声明为私有,并将我的 setter 方法声明为公共...

ListAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.setCurrentWidth(100);

Eclipse 也不高兴...我知道这是一个不太可能的事情,但是当你面对奇怪的行为时,你也开始表现得很奇怪:))

)日食侵入性地提供了修正,并且最终起作用了!是以下代码:

((TrainingsListCursorAdapter) listAdapter).setOrientationWidth(100);

有人可以向我解释一下这个语法以及这种转换的必要性吗?! ...以及有什么问题:

  • 实例化一个对象
  • ,访问它的公共 setter/getters 或直接成员变量

I'm facing strange OOP behaviour (IMO) in android and maybe in Java in general...
I have an activity and inside of it I instantiate an object of CustomSimpleCursorAdapter which is a class extending SimpleCursorAdapter. In my custom class I have some member variables added.
When I create an object of that class I want to be able to access a member variable of newly created object. My OOP experience was telling me that the following should suffice:

ListAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.currentWidth = 100;

NOTE: currentWith variable is declared public...

(Eclipse) didn't want to compile, so I thought maybe a public setter setCurrentWidth()in the custom class would solve it. I declared a member variable private and my setter method public....

ListAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.setCurrentWidth(100);

Eclipse wasn't pleased either... I knew it was a long shot, but when you face strange behaviour, you start to behave strange too :)))

The correction that eclipse was intrusively offering and in the end works! is the following code:

((TrainingsListCursorAdapter) listAdapter).setOrientationWidth(100);

Could someone please explain me this syntax and need for such casting?!
...and what's wrong with:

  • instantiate an object
  • access it public setters/getters or directly member variables

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2024-12-22 03:31:41

这一点也不奇怪。变量listAdapter 只是ListAdapter 类型。编译器并不关心前面几行发生了什么赋值。如果您尝试通过 ListAdapter 访问未知的成员,编译器将正确地进行抱怨。

比强制转换更简单的修复方法是将变量声明为您需要的类型,以便访问您的特定成员:

TrainingsListCursorAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.currentWidth = 100;

或者

TrainingsListCursorAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.setOrientationWidth(100);

因此回答您的最后一点 - 实例化对象没有任何问题并访问公共成员。尝试通过不了解这些成员的类型的变量来执行此操作是有问题的。

举一个常见类型中更简单的例子,不能这么写:

Object text = "hello";
int length = text.length();

因为 length()Object 上的方法,它只能在 String 上。当编译器看到表达式 text.length() 时,它会查看变量 text 的声明类型(即本例中的 Object)并查找该类型的 length() 方法。

因此,为了使上述代码正常工作,您需要执行以下操作:

String text = "hello";
int length = text.length();

It's not particularly strange at all. The variable listAdapter is just of type ListAdapter. The compiler couldn't care less what assignments have occurred on previous lines. If you try to access a member which isn't known via ListAdapter, the compiler will correctly complain.

A simpler fix than casting would be to declare the variable as being the type you need it to be in order to access your specific members:

TrainingsListCursorAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.currentWidth = 100;

or

TrainingsListCursorAdapter listAdapter = new TrainingsListCursorAdapter(...);
listAdapter.setOrientationWidth(100);

So to answer your final point - there's nothing wrong with instantiating an object and accessing public members. There is something wrong with trying to do so via a variable of a type which doesn't know about those members.

To give a simpler example in common types, you can't write:

Object text = "hello";
int length = text.length();

because length() is a method on Object, it's only on String. When the compiler sees the expression text.length(), it looks at the declared type of the variable text (i.e. Object in this case) and looks for the length() method on that type.

So this is what you'd do instead to make the above code work:

String text = "hello";
int length = text.length();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文