开发者

Why are parentheses used when declaring a textview

开发者 https://www.devze.com 2023-03-22 08:06 出处:网络
I\'m fairly new to android.I\'ve got experience with c# and java but I can\'t figure this out. I know how to use it.I\'ve written a couple apps already.It\'s probably really simple but I don\'t under

I'm fairly new to android. I've got experience with c# and java but I can't figure this out.

I know how to use it. I've written a couple apps already. It's probably really simple but I don't understand this. Why is (TextView) in parentheses.

I've searched the sdk documentation and afaik it doesn't mention this.

TextView example = (Te开发者_StackOverflow中文版xtView) findViewById(R.id.example);


This is an explict cast, as findViewById() returns the more generic type View.

The convention is from C, and then used in C++, and in this case, Java. VB.net and other BASIC variants have CType(), or CAST(). Most stongly typed languages have some similar mechanism to explicitly convert between types in a compatible way. Note that, in example, not everything returned from findViewById() can be safely cast into the TextView type.

0

精彩评论

暂无评论...
验证码 换一张
取 消