【问题标题】:Meaning of syntax of new object creation of a child class in Java?Java中子类的新对象创建语法的含义?
【发布时间】:2015-04-23 13:52:15
【问题描述】:

我有以下 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,以及一个扩展Parent 并实现Interface1 的子类Child

但是,我对以下陈述有疑问:

 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有什么意义?

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

谢谢。

【问题讨论】:

  • Instanceof 基本上告诉你,如果一个实例可以分配给一个类型。而且由于您可以将 Child 分配给 Parent,并且您可以将实现接口的任何实例分配给该接口,所有这些都是 true。

标签: java class interface instance parent-child


【解决方案1】:

在这种情况下,obj1 是 Parent 或 Child 的一个实例?

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

上述说法究竟是什么意思?

obj1 是一个孩子,正如你定义的,所有 Child 的实例也是 Parent 的实例,所以 obj1 也是 parent 的实例。

=左侧的Parent obj1是什么意思,=右侧的new Child()是什么意思。 Parent obj1 = new Child(); 是什么意思?作为声明?

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

在创建 obj1 时,Parent 单词没有任何区别吗?

是的,如果您将一个方法添加到 Child 而不是 Parent,您将无法在 obj1 上调用它,因为 obj1 可以包含任何父项,而不仅仅是一个子项。

在创建 obj1 的同时写 Parent 有什么意义?

它允许存储任何 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");}
}

【讨论】:

    【解决方案2】:

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

    Parent p = new Child();
    

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

    List a = new ArrayList();
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 2016-07-21
      • 2014-02-21
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多