如何使用instanceof运算符进行向下转换 [英] How to do Downcasting with instanceof Operator

查看:102
本文介绍了如何使用instanceof运算符进行向下转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向下转换是指子类引用父类的对象。我试过没有instanceof运算符。

Downcasting is when child class refers to the object of Parent class. I had tried to do it without the instanceof operator.

    class Food { }  
    class bread4 extends Food 
{  
      static void method(Food a) {  
           bread4 b=(bread4)a;
           System.out.println("ok downcasting performed");  
      }  
       public static void main (String [] args) {  
        Food a=new bread4();  
        bread4.method(a);  
      }  
    }  





任何人都可以通过使用instanceof运算符来帮助我吗



Can anyone help me in this by using instanceof operator

推荐答案

Java中的Instanceof操作符



java中的instanceof也称为类型比较运算符,因为它将实例与类型进行比较。它返回true或false。如果我们将instanceof运算符应用于任何具有null值的变量,则返回false。



class Animal {}

class Dog1 extends Animal

{

public static void main(String args [])

{

Dog1 d = new Dog1( );

System.out.println(d instanceof Animal); // true

}

}

向下转发instanceof运算符 - 更多示例



当Subclass类型引用Parent类的对象时,它被称为向下转换。如果我们直接执行它,编译器会给出编译错误。如果通过类型转换执行它,则会在运行时抛出ClassCastException。但是如果我们使用instanceof运算符,则可以进行向下转换。



Dog d = new Animal(); //编译错误

如果我们执行通过类型转换进行转发,在运行时抛出ClassCastException。



Dog d =(Dog)new Animal();

//编译成功但运行时抛出ClassCastException

示例:



class Animal {}



类Dog3扩展Animal {

静态void方法(动物a){

if(dog3的实例){

Dog3 d =(Dog3)a; // downcasting

System.out.println(& quot; ok downcasting perform& quot;);

}

}



public static void main(String [] args){

Animal a = new Dog3();

Dog3.method(a);

}



}

输出:执行正确的向下转换< / pre>
Instanceof Operator in Java

The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has null value, it returns false.

class Animal{}
class Dog1 extends Animal
{
public static void main(String args[])
{
Dog1 d=new Dog1();
System.out.println(d instanceof Animal);//true
}
}
Downcasting with instanceof operator - More Example

When Subclass type refers to the object of Parent class, it is known as downcasting. If we perform it directly, compiler gives Compilation error. If you perform it by typecasting, ClassCastException is thrown at runtime. But if we use instanceof operator, downcasting is possible.

Dog d=new Animal();//Compilation error
If we perform downcasting by typecasting, ClassCastException is thrown at runtime.

Dog d=(Dog)new Animal();
//Compiles successfully but ClassCastException is thrown at runtime
Example:

class Animal { }

class Dog3 extends Animal {
static void method(Animal a) {
if(a instanceof Dog3){
Dog3 d=(Dog3)a;//downcasting
System.out.println(&quot;ok downcasting performed&quot;);
}
}

public static void main (String [] args) {
Animal a=new Dog3();
Dog3.method(a);
}

}
Output:ok downcasting performed</pre>

请参阅 java实例 - Google搜索 [ ^ ]。


这篇关于如何使用instanceof运算符进行向下转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆