Java程式設計中的構造器鏈


構造器鏈是在使用者在特定方法中初始化物件時注入構造器的特定序列。當我們一個接一個地呼叫大量構造器時,僅基於例項類,就可以使用此過程。此過程是與繼承相關的另一種方法,其中子類構造器的任務是呼叫超類構造器。

在Java中可以透過兩種方式執行構造器鏈:

  • 在同一個類中 - 此過程可以透過使用`this()`關鍵字來完成,該關鍵字用於同一個類中的構造器。

  • 從基類 - 透過使用`super()`關鍵字,我們可以從基類呼叫構造器。

這是一個例子:

ConstructorChaining(int x){
	System.out.println("Parameterized (First Parameter) Constructor Of
	Class.");
	System.out.println("The Value Of x Is "+x);
}
ConstructorChaining(int x, int y){
	this();
	System.out.println("Parameterized (Second Parameters) Constructor Of
	The Class.");
	System.out.println("The Value Of x And y Is " + x + "And " + y + ". "
	+ "The Sum Of x And y Is " + (x + y) );
}

在Java中執行構造器鏈的演算法

在這個可能的演算法中,我們將向您展示如何在Java環境中執行構造器鏈過程。透過使用這個可能的演算法,我們將構建一些語法,透過這些語法,我們將能夠用各種方法描述問題陳述。

  • 步驟1 - 開始該過程。

  • 步驟2 - 宣告過程包。

  • 步驟3 - 匯入並使用Java包來執行該過程。

  • 步驟4 - 宣告一個公共類。

  • 步驟5 - 宣告第一個預設構造器。

  • 步驟6 - 此構造器將呼叫同一個類中的另一個構造器。

  • 步驟7 - 呼叫另一個構造器。

  • 步驟8 - 呼叫引數化構造器作為過程。

  • 步驟9 - 宣告`this()`運算子。

  • 步驟10 - 宣告另一個引數化構造器作為對。

  • 步驟11 - 提及print方法。

  • 步驟12 - 宣告一個字串引數。

  • 步驟13 - 首先呼叫預設構造器。

  • 步驟14 - 為了進一步呼叫無引數構造器。

  • 步驟15 - 獲取Derived obj = new Derived()的值。

  • 步驟16 - 獲取返回值並終止該過程。

在Java中執行構造器鏈的語法

class A {
	public int a;
	public A() {
		this(-1);
	}
	public A(int a) {
		this.a = a;
	}
	public String toString() {
		return "[ a= " + this.a + "]";
	}
}
class B extends A {
	public int b;
	public B() {
		this(-1,-1);
	}
	public B(int a, int b) {
		super(a);
		this.b = b;
	}
	public String toString() {
		return "[ a= " + this.a + ", b = " + b + "]";
	}
}
public class Tester {
	public static void main(String args[]) {
		A a = new A(10);
		System.out.println(a);
		B b = new B(11,12);
		System.out.println(b);
		A a1 = new A();
		System.out.println(a1);
		B b1 = new B();
		System.out.println(b1);
	}
}

在上面這個可能的語法中,我們試圖向您展示如何在Java環境中建立和執行構造器鏈方法。透過使用上面提到的語法,我們正在朝著一些可能的解決方法前進,以有效的方式解決問題陳述。

遵循的方法

  • 方法1 - Java程式演示使用`this()`、`super()`、初始化塊關鍵字在同一個類中進行構造器鏈

  • 方法2 - Java程式構建一個邏輯,其中一個構造器使用`this`關鍵字呼叫另一個構造器,並在物件上進行操作

方法1:使用`this()`、`super()`、初始化塊關鍵字演示在同一個類中進行構造器鏈

`this()`方法的使用

在這個可能的方法中,我們將應用`this()`方法來演示在同一個類中進行構造器鏈。

public Person(String firstName, String middleName, String lastName, int age) {
	this.firstName = firstName;
	this.middleName = middleName;
	this.lastName = lastName;
	this.age = age;
}

示例

//Java program to illustrate the constructor chaining within same class Using this() keyword
public class ARBRDD{
   ARBRDD(){
      this(7);
      System.out.println("The Default Constructor Value Is Here!");
   }
   ARBRDD(int x){
      this(7, 16);
      System.out.println(x);
   }
   ARBRDD(int x, int y){
      System.out.println(x * y);
   }
   public static void main(String args[]){
      new ARBRDD();
   }
}

輸出

112
7
The Default Constructor Value Is Here!

構造器順序交換方法的使用

在這個可能的方法中,我們將應用`this()`關鍵字來演示在同一個類中進行構造器鏈。透過這個,我們還可以部署構造器的更改順序。

示例

//Java program to illustrate Constructor Chaining within same class Using this() keyword and also deploy the changing order of constructors
public class ARBRDD{
   ARBRDD(){
      System.out.println("Default Value Is Here!");
   }
   ARBRDD(int x){
      this();
      System.out.println(x);
   }
   ARBRDD(int x, int y){
      this(5);
      System.out.println(x * y);
   }
   public static void main(String args[]){
      new ARBRDD(7, 16);
   }
}

輸出

Default Value Is Here!
5
112

`super()`方法的使用

在這個可能的方法中,我們將應用`super()`類來演示對其他類的構造器鏈。

示例

//Java program to illustrate Constructor Chaining to other class using super() keyword
class ARBRDD{
   String name;
   ARBRDD(){
      this("");
      System.out.println("No-argument Constructor Of The" + " Base Class Is Here");
   }
   ARBRDD(String name){
      this.name = name;
      System.out.println("Calling The Parameterized Constructor" + " Of The Base Class>>");
   }
}
public class Derived extends ARBRDD{
   Derived(){
      System.out.println("No-argument Constructor Value " +
      "Of Derived Here");
   }
   Derived(String name){
      super(name);
      System.out.println("Calling The Parameterized " +
      "Constructor Of Derived Value!!");
   }
   public static void main(String args[]){
      Derived obj = new Derived("TEST");
   }
}

輸出

Calling The Parameterized Constructor Of The Base Class>>
Calling The Parameterized Constructor Of Derived Value!!

初始化塊方法的使用

在這個可能的方法中,我們將應用初始化塊方法,透過該方法,將與每個構造器值一起執行公共資源。

示例

//Java program to demonstrate using Init block where common resources to be executed with the every constructor value
public class ARBRDD{{
      System.out.println("Init Block Value Is Here Now!");
   }
   ARBRDD(){
      System.out.println("Default Value Displaying Now!!");
   }
   ARBRDD(int x){
      System.out.println(x);
   }
   public static void main(String[] args){
      new ARBRDD();
      new ARBRDD(071610);
   }
}

輸出

Init Block Value Is Here Now!
Default Value Displaying Now!!
Init Block Value Is Here Now!
29576

阻塞移除方法的使用

在這個可能的方法中,我們將應用阻塞方法,以便在第一次阻塞發生後從阻塞中移除元素。

示例

//Java program to perform the removing the blockage to be executed after the first block
public class ARBRDD{{
      System.out.println("Init Value");
   }
   ARBRDD(){
      System.out.println("Default Value Is Here");
   }
   ARBRDD(int x){
      System.out.println(x);
   }{
      System.out.println("Second Value Is Here Now!");
   }
   public static void main(String args[]){
      new ARBRDD();
      new ARBRDD(071610);
   }
}

輸出

Init Value
Second Value Is Here Now!
Default Value Is Here
Init Value
Second Value Is Here Now!
29576

方法2:構建一個邏輯,其中一個構造器呼叫另一個構造器

另一個構造器呼叫方法的使用

在這個可能的方法中,我們將應用一個邏輯,其中一個構造器使用`this`關鍵字呼叫另一個構造器來構建一個員工記錄管理系統。

public Customer(String firstName, String lastName, int age, String CardId) {
   this(firstName, null, lastName, age, CardId);
}
public Customer(String firstName, String middleName, String lastName, int age,
String CardId) {
   super(firstName, middleName, lastName, age);
   this.CardId = CardId;
}

示例1

//Java program to build a logic where one constructor is calling another constructor using this keyword.
public class Employee{
   public String empName;
   public int empSalary;
   public String address;
   public Employee(){
      this("ARB");
   }
   public Employee(String name){
      this(name, 07102001);
   }
   public Employee(String name, int sal){
      this(name, sal, "DINAJPUR");
   }
   public Employee(String name, int sal, String addr){
      this.empName=name;
      this.empSalary=sal;
      this.address=addr;
   }
   void disp(){
      System.out.println("Employee Name Is: "+empName);
      System.out.println("Employee Total Salary Is: "+empSalary);
      System.out.println("Employee Parmanent Address Is: "+address);
   }
   public static void main(String[] args){
      Employee obj = new Employee();
      obj.disp();
   }
}

輸出

Employee Name Is: ARB
Employee Total Salary Is: 1868801
Employee Parmanent Address Is: DINAJPUR

示例2

//Java code to declare a constructor without arguments which is invoked when we create an object and perform the constructor chaining
class Constructer{
   Constructer(){
      System.out.println("Constructer With The Zero Value Arguments");
   }
   Constructer(int x){
      System.out.println("Constructer With The Argument One Type Int Value "+"("+x +")");
   }
   Constructer(double x){
      System.out.println("Constructer With The Argument Of The One Type Double Value"+"("+x +")");
   }
   Constructer(int x,int y){
      System.out.println("Constructer With Two Arguments Of The Type Int"+"("+x+","+y+")");
   }
   Constructer(double x,double y){
      System.out.println("Constructer With Two Argument Type Double"+"("+x+","+y+")");
   }
   Constructer(double x,int y){
      System.out.println("Constructer With Two Arguments Type Double and Int Value "+"("+x+","+y+")");
   }
   Constructer(int x,double y){
      System.out.println("Constructer With Two Arguments Type Int and Double"+"("+x+","+y+")");
   }
   Constructer(int x,int y,double z){
      System.out.println("Constructer With Three Arguments Two Of Type Int and One Is Double"+"("+x+","+y+","+z+")");
   }
}
public class ARBRDD{
   public static void main(String args[]){
      Constructer c1=new Constructer();
      Constructer c2=new Constructer(1);
      Constructer c3=new Constructer(1.0);
      Constructer c4=new Constructer(2,3);
      Constructer c5=new Constructer(3.4,4.5);
      Constructer c6=new Constructer(1.2,2);
      Constructer c7=new Constructer(2,3.2);
      Constructer c8=new Constructer(1,2,3);
   }
}

輸出

Constructer With The Zero Value Arguments
Constructer With The Argument One Type Int Value (1)
Constructer With The Argument Of The One Type Double Value(1.0)
Constructer With Two Arguments Of The Type Int(2,3)
Constructer With Two Argument Type Double(3.4,4.5)
Constructer With Two Arguments Type Double and Int Value (1.2,2)
Constructer With Two Arguments Type Int and Double(2,3.2)
Constructer With Three Arguments Two Of Type Int and One Is Double(1,2,3.0)

結論

在Java程式設計領域,構造器鏈是從一個構造器呼叫另一個構造器的過程。在今天的文章中,我們學習了構造器鏈方法。透過使用上面提到的語法和演算法,我們構建了一些Java程式碼來有效地解釋問題陳述。

更新於:2023年12月29日

811 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.