简体   繁体   English

Java中的父子参数化构造函数

[英]Parent and child parameterized constructor in Java

Normal testing for Parent and child constructors in Java, so I use parameterized constructors in both parent and child class, Java中父子构造函数的正常测试,所以我在父类和子类中都使用参数化构造函数,
But I didn't get the right output.但是我没有得到正确的输出。

class A {

    A(int i) {
        System.out.println(i);
    }
}



class B extends A {

    B(int i, int j) {
        System.out.println(i+j);
    }
}



public class Test {

    public static void main(String args[]) {    
        B b = new B(5,7);
    }
}

Class B extends Class A which has a non-empty constructor. B 类扩展了具有非空构造函数的A 类
So when we try to construct an instance of Class B , we should call the parent constructor first所以当我们尝试构造B 类的实例时,我们应该先调用父构造函数
By calling super(...) keyword通过调用super(...)关键字

super(integerValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM