简体   繁体   English

关于 android 开发中使用的 java 语法的问题

[英]Question regarding java syntax used in android development

I was reading a beginners tutorial on android development which had this line in it's code:我正在阅读有关 android 开发的初学者教程,其中包含以下代码:

TextView fortune = (TextView) findViewById(R.id.fortune_text);  

I'm fairly new to Java, but I understand the basics of creating variables.我对 Java 相当陌生,但我了解创建变量的基础知识。 So making a new int variable:所以制作一个新的 int 变量:

int someInt = 4; int someInt = 4;

Looks a bit different from what is going on above.看起来与上面发生的事情有点不同。 So I guess we are creating an instance of the TextView object, and then we are calling the findViewById method from the TextView class?所以我猜我们正在创建一个 TextView object 的实例,然后我们从 TextView ZA2F21ED4F8EBC2ABCBBB4DC 调用 findViewById 方法But why the (TextView) bit?但为什么是 (TextView) 位? What are we telling java to do here?我们告诉 java 在这里做什么?

The casting in your example has to do with inheritance.您的示例中的铸造与 inheritance 有关。 The method findViewById returns an instance/object of the class View - ie it returns a View/widget. findViewById方法返回 class View的实例/对象 - 即它返回一个视图/小部件。 TextView , which is the kind of object you want to get access to, is a subclass of View . TextView是您想要访问的 object 的一种,是View的子类。 The casting bit - (TextView) - is your way to inform the compiler of your intentions, that is that you want access to the extended set of methods provided by a TextView and not just any View .转换位 - (TextView) - 是您告知编译器您的意图的方式,即您希望访问由TextView而不仅仅是任何View提供的扩展方法集。

It would probably be worth checking http://download.oracle.com/javase/tutorial/java/IandI/subclasses.html for more details.可能值得检查http://download.oracle.com/javase/tutorial/java/IandI/subclasses.html了解更多详细信息。

findViewbyId() returns a View class. findViewbyId()返回一个View class。 TextView is a subclass of View , and it's casting it to TextView with the (TextView) part. TextViewView的子类,它使用(TextView)部分将其转换为TextView

Basically, the left part of the statement is what you're assigning - the right part (after =) is the source of the value / object:基本上,语句的左侧部分是您分配的内容 - 右侧部分(在 = 之后)是值的来源 / object:

TextView fortune = (TextView) findViewById(R.id.fortune_text); 

The above line declares an object variable of the TextView type, and assigns it to the return of the method findViewById(String) .上面的行声明了一个 TextView 类型的 object 变量,并将其分配给方法findViewById(String)的返回值。 The (TextView) part ensures that the method return is cast to a TextView object. (TextView)部分确保方法返回被强制转换为 TextView object。 To copy the object, you could write:要复制 object,您可以编写:

TextView someFortune = fortune;

Which is much closer to the syntax of your next line (as you're assigning the same / compatible types).这更接近下一行的语法(因为您正在分配相同/兼容的类型)。

The next line:下一行:

int someInt = 4;

Declares an int , and just assigns its value (without a method call).声明一个int ,并且只分配它的值(没有方法调用)。 Because "4" is an int , there's no need to explicitly cast its type (it's primitive anyway).因为 "4" 是一个int ,所以不需要显式地转换它的类型(无论如何它都是原始的)。

Update:更新:

Explanation of object type casting: object型铸件说明:

http://www.javabeginner.com/learn-java/java-object-typecasting http://www.javabeginner.com/learn-java/java-object-typecasting

So I guess we are creating an instance of the TextView object, and then we are calling the findViewById method from the TextView class?所以我猜我们正在创建一个 TextView object 的实例,然后我们从 TextView ZA2F21ED4F8EBC2ABCBBB4DC 调用 findViewById 方法

The TextView fortune part does not create anything, just declares a variable of type TextView . TextView fortune部分不会创建任何东西,只是声明了一个TextView类型的变量。

The findViewById() method returns a View instance, which is the parent class of all widgets. findViewById()方法返回一个View实例,它是所有小部件的父 class。 In order to use it as a TextView , you have to cast it first.为了将其用作TextView ,您必须先将其投射。

Look at the answer by geek_ji posted here "Why do you at all need a typecast" for a very basic understanding.查看 geek_ji 在此处发布的答案“你为什么需要类型转换”以获得非常基本的理解。

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

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