关于类变量的向上转换和向下转换有什么区别 [英] What is the difference between up-casting and down-casting with respect to class variable

查看:31
本文介绍了关于类变量的向上转换和向下转换有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就类变量而言,向上转型和向下转型有什么区别?

What is the difference between up-casting and down-casting with respect to class variable?

例如在下面的程序类中Animal只包含一个方法而Dog类包含两个方法,那么我们如何将Dog变量转换为Animal变量.

For example in the following program class Animal contains only one method but Dog class contains two methods, then how we cast the Dog variable to the Animal Variable.

如果转换完成,那么我们如何使用Animal的变量调用Dog的另一个方法.

If casting is done then how can we call the Dog's another method with Animal's variable.

class Animal 
{ 
    public void callme()
    {
        System.out.println("In callme of Animal");
    }
}


class Dog extends Animal 
{ 
    public void callme()
    {
        System.out.println("In callme of Dog");
    }

    public void callme2()
    {
        System.out.println("In callme2 of Dog");
    }
}

public class UseAnimlas 
{
    public static void main (String [] args) 
    {
        Dog d = new Dog();      
        Animal a = (Animal)d;
        d.callme();
        a.callme();
        ((Dog) a).callme2();
    }
}

推荐答案

向上转换是转换到超类型,而向下转换是转换到子类型.始终允许向上转换,但向下转换涉及类型检查并可能抛出 ClassCastException.

Upcasting is casting to a supertype, while downcasting is casting to a subtype. Upcasting is always allowed, but downcasting involves a type check and can throw a ClassCastException.

在您的情况下,从 DogAnimal 的转换是向上转换,因为 Dog is-a Animal.一般来说,只要两个类之间存在 is-a 关系,就可以向上转型.

In your case, a cast from a Dog to an Animal is an upcast, because a Dog is-a Animal. In general, you can upcast whenever there is an is-a relationship between two classes.

向下转换是这样的:

Animal animal = new Dog();
Dog castedDog = (Dog) animal;

基本上你正在做的是告诉编译器你知道对象的运行时类型真正是什么.编译器将允许转换,但仍会插入运行时完整性检查以确保转换有意义.在这种情况下,转换是可能的,因为在运行时 animal 实际上是一个 Dog 即使 animal 的静态类型是 Animal.

Basically what you're doing is telling the compiler that you know what the runtime type of the object really is. The compiler will allow the conversion, but will still insert a runtime sanity check to make sure that the conversion makes sense. In this case, the cast is possible because at runtime animal is actually a Dog even though the static type of animal is Animal.

但是,如果您要这样做:

However, if you were to do this:

Animal animal = new Animal();
Dog notADog = (Dog) animal;

你会得到一个 ClassCastException.原因是因为 animal 的运行时类型是 Animal,所以当你告诉运行时执行转换时,它看到 animal 不是不是真正的 Dog,所以会抛出一个 ClassCastException.

You'd get a ClassCastException. The reason why is because animal's runtime type is Animal, and so when you tell the runtime to perform the cast it sees that animal isn't really a Dog and so throws a ClassCastException.

要调用超类的方法,您可以执行 super.method() 或执行向上转换.

To call a superclass's method you can do super.method() or by performing the upcast.

要调用子类的方法,您必须进行向下转换.如上所示,您通常会冒着 ClassCastException 的风险这样做;但是,您可以在执行强制转换之前使用 instanceof 运算符来检查对象的运行时类型,这允许您防止 ClassCastExceptions:

To call a subclass's method you have to do a downcast. As shown above, you normally risk a ClassCastException by doing this; however, you can use the instanceof operator to check the runtime type of the object before performing the cast, which allows you to prevent ClassCastExceptions:

Animal animal = getAnimal(); // Maybe a Dog? Maybe a Cat? Maybe an Animal?
if (animal instanceof Dog) {
    // Guaranteed to succeed, barring classloader shenanigans
    Dog castedDog = (Dog) animal;
}

从 Java 16 开始可以更简洁地表达向下转换,Java 16 引入了 instanceof:

Downcasts can be expressed more succinctly starting from Java 16, which introduced pattern matching for instanceof:

Animal animal = getAnimal(); // Maybe a Dog? Maybe a Cat? Maybe an Animal?
if (animal instanceof Dog castedDog) {
    // now castedDog is available here as in the example above
}

这篇关于关于类变量的向上转换和向下转换有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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