我有下面的 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
,一个接口(interface)Interface1
,以及一个子类 Child
延伸Parent
并实现Interface1
.
但是,我对下面的说法有疑问:
Parent obj1 = new Child();
在本例中,obj1 是 Parent
的实例或Child
?上述说法的真正含义是什么? 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 都是 Parent
的实例, Child
和Interface1
。难道是Parent
创建 obj1 时的 word 有什么区别吗?写作的意义是什么Parent
创建 obj1 时?
我发现很难用语言表达,但我希望你能明白我的问题。
谢谢。
最佳答案
In this case obj1 is an instance of Parent or Child ?
它是 Child 的一个实例,也是 Parent 的一个实例。
What is the meaning of the above statement really ?
obj1 是一个子对象,正如您所定义的那样,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
是一个局部变量,可以保存作为 Parent 实例的任何对象。构造一个 Child 的事实意味着它实际上可以被赋值,因为所有 Child
对象都是 Parent
。
Didnt the Parent word while creating obj1 make any difference ?
是的,如果您向 Child
添加方法,但不向 Parent
添加方法,您将无法在 obj1
上调用该方法,因为 obj1
可以容纳任何父级,而不仅仅是一个子级。
What was the significance of writing Parent while creating obj1 ?
它允许存储任何父级
,甚至不是子级
。
也许以下类的命名(作为类比)可能更有意义:
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");}
}
关于java - Java中子类创建新对象的语法含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28653476/