我们如何能够访问孩子的构造函数

发布于 2025-01-26 02:49:18 字数 867 浏览 1 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天赋异禀 2025-02-02 02:49:18

而不是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 of Parent and Child.

眼角的笑意。 2025-02-02 02:49:18

您正在混淆两个完全不同的事情。

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 that obj 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 a fun() 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 call new 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文