android访问扩展customListAdapter的成员变量
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一点也不奇怪。变量
listAdapter
只是ListAdapter
类型。编译器并不关心前面几行发生了什么赋值。如果您尝试通过ListAdapter
访问未知的成员,编译器将正确地进行抱怨。比强制转换更简单的修复方法是将变量声明为您需要的类型,以便访问您的特定成员:
或者
因此回答您的最后一点 - 实例化对象没有任何问题并访问公共成员。尝试通过不了解这些成员的类型的变量来执行此操作是有问题的。
举一个常见类型中更简单的例子,不能这么写:
因为
length()
是Object
上的方法,它只能在String
上。当编译器看到表达式text.length()
时,它会查看变量text
的声明类型(即本例中的Object
)并查找该类型的length()
方法。因此,为了使上述代码正常工作,您需要执行以下操作:
It's not particularly strange at all. The variable
listAdapter
is just of typeListAdapter
. The compiler couldn't care less what assignments have occurred on previous lines. If you try to access a member which isn't known viaListAdapter
, 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:
or
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:
because
length()
is a method onObject
, it's only onString
. When the compiler sees the expressiontext.length()
, it looks at the declared type of the variabletext
(i.e.Object
in this case) and looks for thelength()
method on that type.So this is what you'd do instead to make the above code work: