我们如何能够访问孩子的构造函数
public class Main {
public static void main(String[] args) {
Parent obj=new Child(1,2,3); //child constructor accessed during object creation
System.out.println(obj.p);
obj.fun()//error->how can I access constructor of child but not function of child
}
}
public class Parent {
int p;
Parent(int p){
this.p=p;
System.out.println("in parent");
}
}
public class Child extends Parent {
int c1,c2;
Child(int c1,int c2,int p){
super(p);
this.c1=c1;
this.c2=c2;
System.out.println("in child");
}
public void fun(){
System.out.println("fun");
}
}
Q>我知道,当变量类型是父对象,而对象类型是儿童时,我们只能访问父的变量和方法,而不是孩子的变量。我的问题是,我们如何能够访问孩子的构造函数?
public class Main {
public static void main(String[] args) {
Parent obj=new Child(1,2,3); //child constructor accessed during object creation
System.out.println(obj.p);
obj.fun()//error->how can I access constructor of child but not function of child
}
}
public class Parent {
int p;
Parent(int p){
this.p=p;
System.out.println("in parent");
}
}
public class Child extends Parent {
int c1,c2;
Child(int c1,int c2,int p){
super(p);
this.c1=c1;
this.c2=c2;
System.out.println("in child");
}
public void fun(){
System.out.println("fun");
}
}
Q> I know that when variable type is parent object and object type is child, we can access the variables and methods of parent only and not that of child. My question is then how are we being able to access constructor of child?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是
parent object =新孩子(1,2,3);
,do
child object = new Child(1,2,3);
现在现在
对象< /代码>将具有
parent> parent
和儿童
的所有方法和字段。Instead of
Parent object = new Child(1, 2, 3);
,Do
Child object = new Child(1, 2, 3);
Now
object
will have all methods and fields ofParent
andChild
.您正在混淆两个完全不同的事情。
obj.fun()
无法调用,因为您已经告诉编译器obj
包含一个可能是类型parent的对象,或任何可能的未知子类。从理论上讲,obj
可以重新分配,因此它包含一个对象,其类型是父母的其他子类,该子类完全没有fun()
方法,因此编译器不能假设存在这种方法。这与构造函数无关。从定义上讲,班级的构造函数使您可以“从无到有创建一个实例”。他们不需要任何类型的现有对象。〜
您可以呼叫
new Child(1,2,3)
,其原因是您可以调用new StringBuilder()
而无需调用 具有现有的StringBuilder实例(或其任何Sublcasses或SuperClasses)。1。实际上,非静态内类的构造函数需要现有实例,但这是一个罕见的情况。
You are confusing two entirely different things.
obj.fun()
cannot be called because you have told the compiler thatobj
contains an object which may be of type Parent, or any possible unknown subclass. In theory,obj
could be reassigned so it contains an object whose type is some other subclass of Parent which doesn’t have afun()
method at all, so the compiler cannot assume such a method exists.This has nothing to do with a constructor. The constructors of a class, by definition, allow you to “create an instance from nothing.” They do not require an existing object of any type.¹
You can call
new Child(1,2,3)
for the same reason you can callnew StringBuilder()
without having an existing instance of StringBuilder (or any of its sublcasses or superclasses).1. Actually, a constructor of a non-static inner class would require an existing instance, but that’s an uncommon case.