有 Java 编程相关的问题?

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

Java中创建子类的新对象的语法含义?

下面是我在学习java教程时编写的java程序

public class Primitive_prac {

   public static void main(String[] args) {
    // TODO code application logic here

    Parent obj1 = new Child();
    Child obj2 = new Child();

    System.out.println("ojb1 is instance of Parent: " + (obj1 instanceof Parent));
    System.out.println("ojb1 is instance of Parent: " + (obj1 instanceof Child));
    System.out.println("ojb1 is instance of Parent: " + (obj1 instanceof Interface1));

    System.out.println();

    System.out.println("ojb2 is instance of Parent: " + (obj2 instanceof Parent));
    System.out.println("ojb2 is instance of Parent: " + (obj2 instanceof Child));
    System.out.println("ojb2 is instance of Parent: " + (obj2 instanceof Interface1));

    }
}

class Parent{}
interface Interface1{}
class Child extends Parent implements Interface1{}

如您所见,我创建了一个类Parent,一个接口Interface1,以及一个子类Child,它扩展了Parent并实现了Interface1

然而,我对以下声明有疑问:

 Parent obj1 = new Child();

在这种情况下,obj1是ParentChild的实例?这句话到底是什么意思?在{}左边的{}是什么意思,在{}右边的{}是什么意思。那么Parent obj1 = new Child();作为一种陈述的含义是什么

下面是程序的输出

run:
ojb1 is instance of Parent: true
ojb1 is instance of Parent: true
ojb1 is instance of Parent: true

ojb2 is instance of Parent: true
ojb2 is instance of Parent: true
ojb2 is instance of Parent: true
BUILD SUCCESSFUL (total time: 0 seconds)

我有点困惑,因为输出显示obj1和obj2都是ParentChildInterface1的实例。创建obj1时的Parent单词没有任何区别吗?在创建obj1时编写Parent的意义是什么

我发现很难用语言表达,但我希望你明白我的问题

谢谢


共 (2) 个答案

  1. # 1 楼答案

    将对象指定给名称时,如中所示:

    Parent p = new Child();
    

    名称“p”指的是一个“子”对象,尽管名称表示它是父对象。这很常见,尽管示例中类型名称的选择可能会产生误导。一个更好的现实例子是:

    List a = new ArrayList();
    

    在本例中,您通过更通用的“列表”引用特定的“ArrayList”对象。这是您在实际代码中经常看到的情况,一般来说,您希望将其指定给最不特定的类型,以便使代码最灵活

  2. # 2 楼答案

    In this case obj1 is an instance of Parent or Child ?

    它是Child的一个实例,也是Parent的一个实例

    What is the meaning of the above statement really ?

    obj1是子对象,正如您定义的,所有子对象的实例都是父对象的实例,因此obj1也是父对象的实例

    What is the meaning of Parent obj1 which is on the left side of = and what is the meaning of new Child()which is on the right side of =. And what is the meaning of Parent obj1 = new Child(); as a statement ?

    Parent obj1意味着obj1是一个局部变量,可以保存任何作为父对象实例的对象。构造子对象的事实意味着它实际上可以被赋值,因为所有Child对象都是Parents

    Didnt the Parent word while creating obj1 make any difference ?

    是的,如果你在Child中添加一个方法,而不是在Parent中添加,你将无法在obj1上调用它,因为obj1可以容纳任何父对象,而不仅仅是子对象

    What was the significance of writing Parent while creating obj1 ?

    它允许存储任何Parent,即使不是Child

    也许下面的类命名(作为类比)更有意义:

    public class Primitive_prac {
    
       public static void main(String[] args) {
        // TODO code application logic here
    
        Animal obj1 = new Bird();
        Bird obj2 = new Bird();
        obj1.move(); //valid, since obj1 is declared `Animal`
        obj1.fly(); // invalid, since obj1 could be any animal and not all animals implement fly();
        obj2.move(); // Valid, all birds are animals and all animals (as defined here) move, therefore birds can move
        obj2.fly(); //valid, obj2 can only be a bird or a subclass, and all such objects can fly()
    
        System.out.println("ojb1 is instance of Animal: " + (obj1 instanceof Animal));
        System.out.println("ojb1 is instance of Bird: " + (obj1 instanceof Bird));
        System.out.println("ojb1 is instance of Flying: " + (obj1 instanceof Flying));
    
        System.out.println();
    
        System.out.println("ojb2 is instance of Bird: " + (obj2 instanceof Animal));
        System.out.println("ojb2 is instance of Animal: " + (obj2 instanceof Bird));
        System.out.println("ojb2 is instance of Flying: " + (obj2 instanceof Flying));
    
        }
    }
    
    class Animal{ void move("Moving...") {} }
    interface Flying{ void fly(); }
    class Bird extends Animal implements Flying{
        void fly() {System.out.println("flap flap");}
    }