简体   繁体   English

Java中的类转换

[英]Class casting in java

In my code two classes are there ,subclass extends superclass.In sub class i override superclass method. 在我的代码中有两个类,子类扩展了超类。在子类中,我重写了超类方法。 On the time of object creation i created superclass reference to sub class object its working fine. 在创建对象时,我创建了对子类对象的超类引用,其工作正常。 But again i converted super class reference to complete super class object but it calls sub class methodn not super class method. 但是我再次将超类引用转换为完整的超类对象,但它调用子类methodn而不是超类方法。 My assumption the output is wrong.Here my code 我假设输出错误,这是我的代码

public class OverRiding {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //Super class reference to sub class Object
        Dog dog = new Animal();
        dog.eat();
        dog.bow();

        //Completely converted into pure Dog class Object
        Dog d = (Dog) dog;
        d.eat();
        d.bow();
    }
}

Dog class 狗类

class Dog {
    public void eat() {
        System.out.println("Dog eat Biscuits");
    }

    public void bow() {
        System.out.println("Dog bow");
    }
}

Class Animal 类动物

class Animal extends Dog {
    public void eat() {
        System.out.println("Animal eat Food");
    }
}

And my output is 我的输出是

Animal eat Food
Dog bow
Animal eat Food
Dog bow

Your concept is not correct bro... 您的想法不正确吗?

 class Animal extends Dog

How can Animal extend a dog? 动物如何延长狗的身分? . Its like saying - "EVERY ANIMAL IS A DOG" . 就像在说“每个动物都是狗”。 Which is wrong... So, it should be the other way around... 错了...所以应该反过来...

This example will only confuse you further.. 这个例子只会使您进一步困惑。

Donno why nobody brought this up... Donno为什么没人提起这个...

 Dog d = (Dog) dog;

Here d is not pure Dog object. 这里d不是纯Dog对象。 It's still of Animal type. 它仍然是Animal型的。 Pure Dog object would be created via Dog d = new Dog() . Pure Dog对象将通过Dog d = new Dog()

   Dog dog = new Animal();
    //Completely converted into pure Dog class Object
    Dog d = (Dog) dog;

Both Dog and d are pointing to the same reference, Since you are not creating a new object . Dogd都指向同一参考,因为您没有创建 object Cast won't have an effect here. 演员表不会在这里起作用。

Casting and Creating is completely different. 投射和创建完全不同。

In this example, you've only created one object, and it's an Animal , (which sadly, is a special type of Dog ). 在此示例中,您仅创建了一个对象,并且是Animal (很遗憾,这是Dog一种特殊类型)。 When you call eat on this object, you're always going to get the version defined in the Animal class, not the version defined in the Dog class. 当您在此对象上调用eat时,总是会获得Animal类中定义的版本,而不是Dog类中定义的版本。

That's polymorphism. 那是多态性。 The version of the method that gets called depends on the class of the object, not the type of the variable that refers to it. 所调用方法的版本取决于对象的类,而不是引用该对象的变量的类型。

You assumption is false: 您的假设是错误的:

Completely converted into pure Dog class Object 完全转换为纯Dog类对象

When you are casting your Animal to a Dog , your variable d is still an Animal . 当您将Animal Dog ,变量d仍然是Animal It is also a Dog , but the most specific type is Animal . 它也是Dog ,但最具体的类型是Animal

You are not completely converted your variable to another instance. 您尚未将变量完全转换为另一个实例。 Your instance is still the same. 您的实例仍然相同。

For a proof, you can try this code : 作为证明,您可以尝试以下代码:

Dog d = (Dog) dog;
d.getClass(); // output Animal

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

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