CodeGym /หลักสูตร /จาวาคอร์ /การสร้างวัตถุ: ลำดับของการเรียกตัวสร้าง

การสร้างวัตถุ: ลำดับของการเรียกตัวสร้าง

จาวาคอร์
ระดับ, บทเรียน
มีอยู่

"สวัสดี Amigo! ตอนนี้ฉันจะบอกคุณเกี่ยวกับวิธีการสร้างวัตถุ"

"มันซับซ้อนอะไรขนาดนั้น ลุงริชิ เขียนชื่อคลาสใหม่ ระบุตัวสร้างที่ถูกต้อง เสร็จแล้ว!"

"นั่นก็จริง แต่จะเกิดอะไรขึ้นภายในวัตถุเมื่อคุณทำเช่นนั้น"

"เกิดอะไรขึ้น?"

"นี่คือสิ่งที่เกิดขึ้น: วัตถุถูกสร้างขึ้นในหลายขั้นตอน"

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; ←-
 }
}
ประกาศสองคลาส: สัตว์เลี้ยง(สัตว์เลี้ยง) และ แมว(แมว)

ในคลาส Cat เราจะเห็นการเรียกใช้ คอน สตรัคเตอร์ของคลาสฐาน อย่างชัดเจน
ควรอยู่ในบรรทัดแรกในตัวสร้างเสมอ

นี่คือสิ่งที่เกิดขึ้นหลังจากจัดสรรหน่วยความจำแล้ว:
18  – เรียกตัวสร้างของคลาสพื้นฐาน
3, 4  – เริ่มต้นตัวแปรใน Pet.
8, 9  – รันโค้ดในตัวสร้างสัตว์เลี้ยง

จากนั้นคลาส 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;
 }
}

"ตอนนี้มันชัดเจนมากขึ้น: ขั้นแรกเป็นคลาสพื้นฐาน จากนั้นตัวแปรนอกตัวสร้าง จากนั้นจึงตามด้วยรหัสตัวสร้าง"

"ทำได้ดีมาก Amigo! แค่นั้นแหละ!"

4
งาน
จาวาคอร์, ระดับ, บทเรียน
ล็อค
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