简体   繁体   English

如何评估? (犯罪)(getListAdapter())。getItem(位置)

[英]How is this get evaluated? (Crime)(getListAdapter()).getItem(position)

My Java is quite a bit rusty and I am currently learning Android. 我的Java有点生锈,我目前正在学习Android。 I encountered this particular line from the book I am reading: 我从正在阅读的书中遇到了这一行:

(Crime)(getListAdapter()).getItem(position).

From what I understand is that this will convert the returned item ( Object type) from getItem() to Crime . 据我了解,这会将返回的项目( Object类型)从getItem()Crime The question here is why is (getListAdapter()) enclosed in parenthesis? 这里的问题是为什么(getListAdapter())括在括号中? Is there a meaning on that expression? 这个表达有意思吗? Is this the same as (Crime) getListAdapter().getItem(position)? 这和(Crime)getListAdapter()。getItem(position)一样吗? I knew that is not just Java but I am perplexed when I encountered this. 我知道这不仅是Java,而且在遇到此问题时会感到困惑。

At this point I am confuse on the semantics of this kind of expression z = (x)(y). 在这一点上,我对这种表达式z = (x)(y).的语义感到困惑z = (x)(y).

Thank you! 谢谢!

It's being used to allow the cast to complete without starting a new expression. 它用于允许转换完成而无需启动新表达式。 That allows the subsequent expression of getItem(position) to be on a Crime . 这样就可以使getItem(position)的后续表达式位于Crime It is equivalent to something like 相当于

Crime c = (Crime) getListAdapter(); // <-- parenthesis not needed
c.getItem(position); // <--  on the Crime

(Crime)(getListAdapter()).getItem(position) is the same thing as (Crime) getListAdapter().getItem(position) (Crime)(getListAdapter()).getItem(position)(Crime) getListAdapter().getItem(position)

But if getItem method is defined in the Crime class and the type returned by getListAdapter() is a java.lang.Object , this code won't compile. 但是,如果在Crime类中定义了getItem方法,并且getListAdapter()返回的类型是java.lang.Object ,则此代码将无法编译。 In fact, the dot following the getListAdapter() will give access to all methods available in java.lang.Object only. 实际上, getListAdapter()之后的getListAdapter()仅允许访问java.lang.Object可用的所有方法。

Maybe this line is wrong and the author wanted to write: ((Crime)getListAdapter()).getItem(position) 也许这行是错误的,并且作者想写: ((Crime)getListAdapter()).getItem(position)

In that case any dot following ((Crime)getListAdapter()) will give you access to all methods available in Crime class. 在这种情况下, ((Crime)getListAdapter())之后的任何点都将使您能够访问Crime类中可用的所有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM