CodeGym /課程 /Java 核心 /創建對象:構造函數調用的順序

創建對象:構造函數調用的順序

Java 核心
等級 5 , 課堂 5
開放

“你好,阿米戈!現在我要告訴你對像是如何創建的。”

“這有什麼複雜的,Rishi叔叔?你寫new和類名,指明正確的構造函數,你就完成了!”

“那是真的。但是當你這樣做時,物體內部會發生什麼?”

“會發生什麼?”

“事情是這樣的:對像是分幾個階段創建的。”

1) 首先,為類的所有成員變量分配內存。

2) 然後初始化基類。

3) 然後所有的變量都賦值,如果指定的話。

4) 最後調用構造函數。

“看起來並不難:首先是變量,然後是構造函數。”

“讓我們看看它在兩個類的示例中是如何工作的:”

代碼 描述
class Pet
{
 int x = 5, y = 5; ←-
 int weight = 10; ←-

 Pet(int x, int y)
 {
  this.x = x; ←-
  this.y = y; ←-
 }
}
class Cat extends Pet
{
 int tailLength = 8; ←-
 int age;
 Cat(int x, int y, int age)
 {
  super(x, y); ←-
  this.age = age; ←-
 }
}
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
          
      
聲明兩個類:Pet(pet) 和 Cat(cat)。

在 Cat 類中,我們看到對基類的構造函數的顯式調用。
它應該始終在構造函數的第一行。

下面是分配內存後發生的事情:
18  – 調用基類的構造函數。
3、4—— 初始化 Pet 中的變量。
8、9  – 執行 Pet 構造函數中的代碼。

然後Cat類開始初始化。
14  – 初始化 Cat 中的變量
19  – 執行 Cat 構造函數中的代碼

public static void main(String[] args)
{
 Cat cat = new Cat (50, 50, 5);
}

“這有點令人困惑。為什麼這麼複雜?”

“如果你知道到底發生了什麼,其實並不難:”

如果一個類沒有任何構造函數,則會自動創建一個。

默認構造函數
class Cat
{
 int x = 5;
 int y = 5;
}
class Cat
{
 int x = 5;
 int y = 5;
 public Cat() 
 {
 }
}

如果不調用基類構造函數,會自動調用。

調用基類的構造函數
class Pet
{
 public String name;
}
class Pet extends Object
{
 public String name;
 public Pet()
 {
  super();
 }
}
class Cat extends Pet
{
 int x = 5;
 int y = 5;
}
class Cat extends Pet
{
 int x = 5;
 int y = 5;
 public Cat() { super(); }
}

成員變量在構造函數中初始化。

成員變量的初始化
class Cat
{
 int x = 5; int y = 5;
}
class Cat
{
 int x; int y;
 public Cat() { super();
  this.x = 5; this.y = 5;
  }
}
到底發生了什麼
class Pet
{
 int x = 5, y = 5;
 int weight = 10;
 Pet(int x, int y)
 {
  this.x = x; this.y = y;
 }
}

class Cat extends Pet
{
 int tailLength = 8;
 int age;
 Cat(int x, int y, int age)
 {
  super(x, y);
  this.age = age;
 }
}
class Pet extends Object
{
 int x;
 int y;
 int weight;

 Pet(int x, int y)
 {
  //call of the base class's constructor
  super();
  //initialize variables
  this.x = 5; this.y = 5; this.weight = 10;
  //execute the constructor code
  this.x = x; this.y = y;
 }
}
class Cat extends Pet
{
 int tailLength;
 int age;
 Cat(int x, int y, int age)
 {
  //call of the base class's constructor
   super(x, y);
  //initialize variables
  this.tailLength = 8;
  //execute the constructor code
  this.age = age;
 }
}

“現在更清楚了:首先是基類,然後是構造函數之外的變量,然後是構造函數代碼。”

“幹得好,阿米戈!就是這樣!”

4
任務
Java 核心, 等級 5, 課堂 5
上鎖
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and assimilate some material. What's more, you combat laziness.
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION