I have a misunderstanding about typecasting in Java language. The problem is ClassCastException. For example, in this code, assuming Animal is the parent class of the Dog class,
我对Java语言的类型转换有误解。问题是ClassCastException。例如,在这个代码中,假设Animal是Dog类的父类,
Animal animal = new Animal();
Dog dog = (Dog) animal;
throws ClassCastException after execution. However, while studying android packages, I found an example about typecasting which should throw a ClassCastException, considering that java example.
执行后抛出ClassCastException。然而,在研究android包时,考虑到java示例,我发现了一个关于类型化的示例,它应该抛出ClassCastException。
EditText editText = (EditText) findViewById(R.id.edit_message);
In this code, findViewById method returns a View class object, which is one of the superclasses of EditText class.(from android.view.View to android.widget.EditText) The code runs fine. Could anyone explain if I made a mistake or how this happens?
在这个代码中,findViewById方法返回一个视图类对象,它是EditText类的超类之一。(从android.view。查看android.widget.EditText)代码运行良好。有人能解释一下我犯了什么错误吗?
Thanks in advance.
提前谢谢。
2 个解决方案
#1
9
Once you create an object, you can't change its type. That's why you can't cast an Animal to a Dog.
创建对象后,不能更改它的类型。这就是为什么你不能把动物扔给狗。
However, if you create an object of a sub-class, you can keep a reference to it in a variable of the super-class type, and later you can cast it to the sub-class type.
但是,如果您创建了一个子类的对象,您可以将它的引用保存在一个超类类型的变量中,然后您可以将它转换为子类类型。
This will work :
这将工作:
Animal a = new Dog ();
Dog d = (Dog) a;
In the Android example, you have a layout resource that looks like this :
在Android示例中,您有一个布局资源,如下所示:
<EditText
android:id="@+id/edit_message"
..."/>
This definition will cause Android to create an instance of EditText
, and therefore you can cast the view returned by findViewById
to EditText
. You can't cast it to anything else that isn't a super-type of EditText
.
这个定义将导致Android创建一个EditText实例,因此您可以将findViewById返回的视图转换为EditText。您不能将它转换为任何非超类型的EditText。
#2
2
Basically you can't cast an instance of a superclass to a subclass because the instance of a subclass is not yet known. Upcasting is a sure way to prevent this exception to happen because we are now dealing polymorphism to our code.
基本上,您不能将超类的实例强制转换为子类,因为子类的实例还不为人知。Upcasting是防止这种异常发生的一种可靠方法,因为我们现在正在处理代码的多态性。
You must instance a subclass first:
您必须首先实例化一个子类:
Dog dog = new Dog;
We can hide the methods of the class Dog not found to its parent class Animal by casting it to its superclass:
我们可以将父类动物没有找到的类狗的方法隐藏到父类动物的超类中:
Animal animal = (Animal) dog;
Then you can downcast this back to your subclass Dog because the instance of its subclass is already known:
然后您可以将它降级到子类Dog,因为它的子类的实例已经是已知的:
Dog anotherDog = (Dog) animal;
#1
9
Once you create an object, you can't change its type. That's why you can't cast an Animal to a Dog.
创建对象后,不能更改它的类型。这就是为什么你不能把动物扔给狗。
However, if you create an object of a sub-class, you can keep a reference to it in a variable of the super-class type, and later you can cast it to the sub-class type.
但是,如果您创建了一个子类的对象,您可以将它的引用保存在一个超类类型的变量中,然后您可以将它转换为子类类型。
This will work :
这将工作:
Animal a = new Dog ();
Dog d = (Dog) a;
In the Android example, you have a layout resource that looks like this :
在Android示例中,您有一个布局资源,如下所示:
<EditText
android:id="@+id/edit_message"
..."/>
This definition will cause Android to create an instance of EditText
, and therefore you can cast the view returned by findViewById
to EditText
. You can't cast it to anything else that isn't a super-type of EditText
.
这个定义将导致Android创建一个EditText实例,因此您可以将findViewById返回的视图转换为EditText。您不能将它转换为任何非超类型的EditText。
#2
2
Basically you can't cast an instance of a superclass to a subclass because the instance of a subclass is not yet known. Upcasting is a sure way to prevent this exception to happen because we are now dealing polymorphism to our code.
基本上,您不能将超类的实例强制转换为子类,因为子类的实例还不为人知。Upcasting是防止这种异常发生的一种可靠方法,因为我们现在正在处理代码的多态性。
You must instance a subclass first:
您必须首先实例化一个子类:
Dog dog = new Dog;
We can hide the methods of the class Dog not found to its parent class Animal by casting it to its superclass:
我们可以将父类动物没有找到的类狗的方法隐藏到父类动物的超类中:
Animal animal = (Animal) dog;
Then you can downcast this back to your subclass Dog because the instance of its subclass is already known:
然后您可以将它降级到子类Dog,因为它的子类的实例已经是已知的:
Dog anotherDog = (Dog) animal;