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

Meaning of syntax of new object creation of a child class in 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{}

如您所见,我创建了一个 class Parent、一个接口 Interface1 和一个扩展 [=15] 的子 class Child =] 并实施 Interface1.

但是,我对下面的说法有疑问:

 Parent obj1 = new Child();

在这种情况下 obj1 是 ParentChild 的一个实例?上面的语句到底是什么意思? =左边的Parent obj1是什么意思,=右边的new Child()是什么意思。 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 有什么意义?

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

谢谢。

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

它是Child的实例,也是Parent的实例。

What is the meaning of the above statement really ?

obj1 是一个 child,正如您定义的那样,Child 的所有实例也是 Parent 的实例,因此 obj1 也是 parent 的实例。

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 是一个局部变量,可以保存任何 object 是 Parent 的实例。您构建 Child 的事实意味着它实际上可以被分配,因为所有 Child object 都是 Parent

Didnt the Parent word while creating obj1 make any difference ?

是的,如果您将方法添加到 Child 但不添加到 Parent,您将无法在 obj1 上调用它,因为 obj1 可以容纳任何Parent,不只是 Child.

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");}
}

当您将对象分配给名称时:

Parent p = new Child();

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

List a = new ArrayList();

在这种情况下,您指的是更通用的 'List' 特定的 'ArrayList' 对象。这是您在实际代码中经常看到的东西,通常您希望分配给最不具体的类型以使您的代码最灵活。