CodeGym /コース /Java コア /オブジェクトの作成: コンストラクター呼び出しの順序

オブジェクトの作成: コンストラクター呼び出しの順序

Java コア
レベル 5, レッスン 5
使用可能

「こんにちは、アミーゴ!これからオブジェクトがどのように作成されるかを説明します。」

「リシおじさん、何がそんなに複雑なんですか? new とクラス名を書き、正しいコンストラクターを指定すれば完了です!」

「それは本当です。しかし、それを行うとオブジェクトの内部で何が起こるでしょうか?」

"何が起こるのですか?"

「これが起こります。オブジェクトはいくつかの段階で作成されます。」

1) まず、クラスのすべてのメンバー変数にメモリが割り当てられます。

2) 次に、基本クラスが初期化されます。

3) 次に、指定されている場合は、すべての変数に値が割り当てられます。

4) 最後に、コンストラクターが呼び出されます。

「それほど難しいものではないようです。まず変数、次にコンストラクターです。」

「2 つのクラスを使用した例でこれがどのように機能するかを見てみましょう。」

コード 説明
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) の 2 つのクラスを宣言します。

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