有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的多态性问题

我试图理解Java中的多态性,但我无法理解App.java中的某些事情

Q1)为什么我们能够做p2=tree;,即使p2是类Plant的引用,而tree是类Tree的对象

问题2)当我尝试这样做时,为什么它会说Type Mismatch:cannot convert from Plant to Tree

tree=new Plant();

问题3)为什么p2.shed()的输出即使是b2 is true也会导致错误?如果p2tree的实例,它应该能够访问Tree方法

应用程序。java

public class App {
    public static void main(String[]args){
        Plant p1 = new Plant();;
        Plant p2 = p1;

        Tree tree = new Tree();

        boolean b1= tree instanceof Plant;

        p2=tree;    

        boolean b2= p2 instanceof Tree;

        System.out.println(b1+" "+b2);

        p2.grow();

        tree.shed();

       //p2.shed(); ---- Why is this an error?

   }

}

植物。爪哇

public class Plant {
    public void grow(){
        System.out.println("Plant growing");
    }
}

树。爪哇

 public class Tree extends Plant {

    @Override
    public void grow() {
        System.out.println("Tree growing");
    }
    public void shed(){
        System.out.println("Tree shedding");
    }
}

共 (4) 个答案

  1. # 1 楼答案

    p2被定义为Plant,它只能调用Plant类中定义的方法,但使用实例树逻辑。方法shed如何在Plant类中未定义,变量p2(Plant类的)不知道方法shed,因此无法在方法shed中调用

    问候

  2. # 2 楼答案

    我假设您自己编写了这些示例,并且您了解在类中如何使用extends声明。看起来您也在尝试扩展类Plant,但它没有声明为抽象类。无论如何:

    您的观点之一是,tree=new Plant();抛出错误的原因是,对于多态性,您必须从广泛到具体地工作

    树是植物的一种,但植物不是树的一种,所以说plant = new Tree();是正确的,这回答了你的第一个问题

    最后,b2不是的,因为p2是一种植物,因为正如我们前面所说的,植物不是一种树

    但是,它给您一个错误的原因是,由于p2属于Plant类(而不是Tree),因此它只有一个可用的方法,即grow(),并且您试图调用shed()方法,该方法仅对Trees可用

  3. # 3 楼答案

    Q1)为什么我们能够做p2=树;即使p2是类Plant的引用,而tree是类tree的对象

    这是有效的

    The p1, referencing to a Parent Object, cannot be cast to Child because a Parent is not a Child. But a Child is a Parent since the Child inherits all the methods in Parent class. However, if p1 is referencing to a Child object or a subclass of Child, then you can cast p1 to Child.

    Parent p1=new Parent();
    Child c1=new Child();
    p1=c1;
    

    问题2)为什么它会说类型不匹配:当我尝试这样做时,无法从一棵植物转换到另一棵树:Tree=newplant()

    Parent p1=new Parent();
    Child c1=new Child();
    c1=(Child)p1;
    

    You cannot cast a Child object to its Parent Though it will compile ,on runtime java identifies that p1 holds a reference which is not assignment compatiable with c1 and will throw java.lang.ClassCastException

    问题3)为什么是p2。shed()即使b2的输出为真,也会出现错误?若p2是树的实例,那个么它应该能够访问树方法

    Reference variable "p2" is of Type Plant and is pointing to an Object of Tree Type. So it will be able to access the method of Plant class

  4. # 4 楼答案

    答复:

    1)植物是树木的父类。 我希望你知道,当我们做Plant p = new Plant();时,p是参考。 并且new Plant()是实际对象

    因此,父引用可以指向子对象因此您可以执行p2=tree

    Plant p2 = new Tree()
    

    2)现在记住我上面写的那一点。父引用可以指向子对象但是子引用不能指向父对象 i、 你不能做Tree t = new Plant();

    3)即使p2实际上指向树对象而不是植物对象,也无法执行p2.shed()。由于引用限制

    参见参考变量“p2”属于植物类型,并且指向树类型的对象但引用p2不知道它指向树类型的对象。它认为它只指向植物类型的对象

    因此,当您执行p2.grow()时,它认为它正在调用Plant类的grow()方法,因为p2是Plant类型的引用

    但当CPU(代码的执行者)转到p2所指向的对象时,CPU发现这实际上是一个树对象,并调用Tree而不是Plant类的grow()方法,因此它打印“Tree is Growing”而不是“Plant is Growing”

    当您执行p2.shed()操作时,会出现编译器错误因为在Plant类中没有称为shed()的方法。。即使p2指向的实际对象可能存在shed()方法的树对象。编译没有发生

    因为引用(p2)不知道子对象中存在的新方法shed()

    阅读::Herbert Schildt-Java是一个完整的参考资料。语言非常简单

    在获得Java方面的一些经验后,请阅读Kathy Sierra SCJP 6以深入了解Java