先决条件—— Java中的构造函数 构造函数链接是从另一个构造函数中调用当前对象的一个构造函数的过程。 构造函数链接可以通过两种方式完成:
- 同班 :可以使用 这个() 同一类中构造函数的关键字
- 从基类: 通过使用 超级() 关键字从基类调用构造函数。
构造函数链接通过 遗产 子类构造函数的任务是首先调用超类的构造函数。这确保了子类对象的创建从超类的数据成员初始化开始。继承链中可以有任意数量的类。每个构造函数调用链,直到到达顶部的类为止。 为什么我们需要构造函数链接? 当我们想在一个构造函数中执行多个任务,而不是在一个构造函数中为每个任务创建一个代码时,可以使用这个过程。我们为每个任务创建一个单独的构造函数,并使它们的链更具可读性。
使用this()关键字在同一类中链接构造函数:
JAVA
// Java program to illustrate Constructor Chaining // within same class Using this() keyword class Temp { // default constructor 1 // default constructor will call another constructor // using this keyword from same class Temp() { // calls constructor 2 this ( 5 ); System.out.println( "The Default constructor" ); } // parameterized constructor 2 Temp( int x) { // calls constructor 3 this ( 5 , 15 ); System.out.println(x); } // parameterized constructor 3 Temp( int x, int y) { System.out.println(x * y); } public static void main(String args[]) { // invokes default constructor first new Temp(); } } |
输出:
755The Default constructor
构造函数链接规则:
- 这个 这个() 表达式应该始终是构造函数的第一行。
- 至少应该有一个构造函数没有this()关键字(上例中的构造函数3)。
- 构造函数链接可以以任何顺序实现。
如果我们改变构造器的顺序会怎么样? 没有什么,构造函数链接可以以任何顺序实现
JAVA
// Java program to illustrate Constructor Chaining // within same class Using this() keyword // and changing order of constructors class Temp { // default constructor 1 Temp() { System.out.println( "default" ); } // parameterized constructor 2 Temp( int x) { // invokes default constructor this (); System.out.println(x); } // parameterized constructor 3 Temp( int x, int y) { // invokes parameterized constructor 2 this ( 5 ); System.out.println(x * y); } public static void main(String args[]) { // invokes parameterized constructor 3 new Temp( 8 , 10 ); } } |
输出:
default580
注意:在示例1中,默认构造函数在最后被调用,但在示例2中,默认构造函数首先被调用。因此,构造函数链接中的顺序并不重要。
构造函数使用super()关键字链接到其他类:
JAVA
// Java program to illustrate Constructor Chaining to // other class using super() keyword class Base { String name; // constructor 1 Base() { this ( "" ); System.out.println( "No-argument constructor of" + " base class" ); } // constructor 2 Base(String name) { this .name = name; System.out.println( "Calling parameterized constructor" + " of base" ); } } class Derived extends Base { // constructor 3 Derived() { System.out.println( "No-argument constructor " + "of derived" ); } // parameterized constructor 4 Derived(String name) { // invokes base class constructor 2 super (name); System.out.println( "Calling parameterized " + "constructor of derived" ); } public static void main(String args[]) { // calls parameterized constructor 4 Derived obj = new Derived( "test" ); // Calls No-argument constructor // Derived obj = new Derived(); } } |
输出:
Calling parameterized constructor of baseCalling parameterized constructor of derived
注:类似于同一类中的构造函数链接, 超级() 应该是构造函数的第一行,因为在子类的构造函数之前调用了超类的构造函数。 替代方法:使用Init块 : 当我们希望使用每个构造函数执行某些公共资源时,我们可以将代码放入 初始块 每当使用构造函数创建新对象时,总是在任何构造函数之前执行.Init块。 例1:
JAVA
class Temp { // block to be executed before any constructor. { System.out.println( "init block" ); } // no-arg constructor Temp() { System.out.println( "default" ); } // constructor with one argument. Temp( int x) { System.out.println(x); } public static void main(String[] args) { // Object creation by calling no-argument // constructor. new Temp(); // Object creation by calling parameterized // constructor with one parameter. new Temp( 10 ); } } |
输出:
init blockdefaultinit block10
注意:如果有多个块,它们将按照在同一类中定义的顺序执行。见前任。 例子:
JAVA
class Temp { // block to be executed first { System.out.println( "init" ); } Temp() { System.out.println( "default" ); } Temp( int x) { System.out.println(x); } // block to be executed after the first block // which has been defined above. { System.out.println( "second" ); } public static void main(String args[]) { new Temp(); new Temp( 10 ); } } |
输出:
initseconddefaultinitsecond10
本文由 阿波娃·辛格 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。