[英]Android Java class casting
I am wondering if someone could explain something about a class cast for me.我想知道是否有人可以为我解释有关 class 演员阵容的一些事情。
I am playing around with Android and I have a subclass of Application named ExApp.我正在玩 Android 并且我有一个名为 ExApp 的应用程序子类。
I want to call a method of ExApp from one of my activities, so I do:我想从我的一个活动中调用 ExApp 的方法,所以我这样做:
ExApp ex = ((ExApp)getapplication());
What I don't understand is why I need a double set of parentheses?我不明白为什么我需要双括号? Why can't I just:
为什么我不能:
ExApp ex = (ExApp)getApplication();
? ?
Thanks.谢谢。
You can.你可以。 The two statements are exactly the same.
这两种说法完全相同。
Where you'd see a difference is if you were calling a method on the result, eg如果您在结果上调用方法,您会看到不同之处,例如
(ExApp) getApplication().foo();
is different to:不同于:
((ExApp) getApplication()).foo();
In the first case, it's the result of foo()
which is cast to ExApp
;在第一种情况下,它是
foo()
的结果,它被强制转换为ExApp
; in the second, it's the result of ExApp
, and the overall expression is the return type of foo()
.第二个是
ExApp
的结果,整体表达式是foo()
的返回类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.