Inheritance in Constructors
เขียนโดย นางสาวสุชาวดี ตู้สมบัติ 650710590
Inheritance เป็นคุณสมบัติที่สำคัญของโปรแกรมภาษาเชิงวัตถุ (object-oriented programming) ที่ทำให้การสร้างคลาสใหม่ ไม่ต้องเขียนซ้ำซ้อนกับคลาสเดิม แต่จะสืบทอดเอาคุณสมบัติและพฤติกรรมของคลาสเดิมมาได้ เช่น เมธอด และ ฟิลล์ ยกเว้น Constructor ไม่ได้สืบทอดโดยตรง แต่เราสามารถเรียก constructor ของคลาสแม่/คลาสฐาน (base class) จาก คลาสลูก/คลาสที่สืบทอด(derived class)ได้ โดยใช้คีเวิร์ด base
example
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
class Employee : Person
{
public int EmployeeId { get; set; }
public Employee(string name, int age, int employeeId) : base(name, age)
{
EmployeeId = employeeId;
}
}จากตัวอย่างข้างต้น Person เป็น base class และ Employee เป็น derived class ซึ่ง constructor ของคลาส Person มีพารามิเตอร์ คือ name และ age ซึ่งเป็นกำหนดค่าเริ่มต้นให้กับฟิลล์ Name และ Age คลาส Employee สืบทอด คลาส Person มาและประกาศ constructor ที่มีพารามิเตอร์ ได้แก่ name , age และ employeeId ซึ่ง มีการเรียก constructor ของ Person โดยใช้คำว่า :base(name, age) เพื่อดึง พารามิเตอร์ name และ age มาใช้นั่นเอง
ตัวอย่างการใช้งาน
output
การใช้ constructor มี 2 กรณีที่น่าพิจารณา ได้แก่
กรณีที่ 1 คลาสลูกมี constructor คลาสแม่ไม่มี constructor ที่เรากำหนดขึ้นมา มันจึงเรียกใช้ default constructor โดยอัตโนมัติ
example
จากตัวอย่างข้างต้น คลาส Tank เป็น base class ให้ข้อมูลเกี่ยวกับขนาดถัง ส่วนคลาส AreaOfTank เป็น derived class ใช้เพิ่มข้อมูลสีและคำนวณพื้นที่ของถึง คลาส Tank ไม่มี constructor ที่กำหนดเอง จึงใช้ default constructor สำหรับสร้างวัตถุของคลาสนี้ และในคลาส AreaOfTank จะมี constructor ที่กำหนดเอง สำหรับสร้างวัตถุของคลาสนี้
กรณีที่ 2 คลาสแม่และคลาสลูกต่างมี constructor ของตัวเอง constructor ของทั้งสองคลาสจึงถูกเรียกใช้งาน ผ่านคำสั่ง base เพื่อเรียก constructor ของคลาสแม่จากภายใน constructor ของคลาสลูก
example
จากตัวอย่างข้างต้น มีbase class และ derived class อันเดียวกับกรณีที่1 แต่ คลาส Tank มี constructor ที่เรากำหนดขึ้นมา ซึ่งมีตัวแปร Radius และ Height และ คลาส AreaOfTank มีการอธิบายสีและพื้นที่ของถัง ดังนั้น constructor ของ คลาส AreaOfTank จึงเรียก constructor ของคลาส Tank โดยส่งพารามิเตอร์ r และ h เข้าไป เพื่อกำหนดค่า Radius และ Height มาใช้ใน คลาส AreaOfTank โดยที่มันไม่ต้องกำหนดค่าเหล่านี้เองอีก
สรุป การใช้ คีย์เวิร์ด base ทำให้การกำหนดค่าในกับ object ของคลาสแม่ทำได้ง่าย และช่วยให้การเรียก constructor ของคลาสแม่จากคลาสลูก ช่วยประหยัดเวลาไม่ต้องเขียนโค้ดซ้ำ
เปรียบเทียบกับภาษา Java/C++/Python
Java
java เรียก คลาสแม่ว่า superclass และเรียกคลาสลูกว่า subclass มีการเรียกใช้ constructor ของคลาสแม่ คล้ายกับ C# แต่จะใช้คำว่า super() ซึ่งใน C# คือคำว่า base() แต่ถ้าไม่ได้เรียก super() มันจะเรียกใช้ default constructor ของคลาสแม่โดยอัตโนมัติ และ การสืบทอดใน java จะใช้คำว่า extends
example
output
จากตัวอย่างข้างต้น มีคลาส Person เป็นคลาสแม่ และ คลาส Student เป็นคลาสลูก ซึ่ง constructor ของ Person มีการแสดงผลว่า "Person class Constructor" ส่วน constructor ของ Student มีการเรียกใช้ super() เพื่อดึงเอา constructor ของ Person มาใช้ และพิมพ์ของตัวเองว่า "Student class Constructor" ใน main เมื่อมีการสร้าง object s จาก คลาส Student มันจึงแสดงผลมาทั้งสองคลาส
C++
C++ มีการเรียกใช้ constructor ของคลาสแม่ คล้าย C# แต่รูปแบบการเขียนไม่เหมือนกัน โดยใน C++ ใช้ชื่อคลาสแม่ในการเรียก constructor ซึ่ง C# ใช้ คำสั่ง base() C++ ถ้าคลาสแม่มี constructor ที่มีการรับพารามิเตอร์ ต้องเรียกจากคลาสลูกด้วยการใช้ initializer list ใน constructor ของคลาสลูก เพื่อส่งค่าไปให้กับ constructor ของคลาสแม่ หากคลาสแม่ไม่มี constructor ที่กำหนดเอง คลาสลูกจะใช้ default constructor ของคลาสแม่โดยอัตโนมัติเช่นเดียวกับภาษา C#
example
output
จากตัวอย่างข้างต้น แสดงให้เห็นว่ามีการใช้ constructor ของคลาสใดบ้าง ถ้ามันสร้าง object จาก คลาสแม่ ผลลัพธ์จะออกมาเป็นคลาสแม่ที่เป็น default ถ้ามันสร้างจากคลาสลูกแบบไม่มีพารามิเตอร์ คือมันจะเรียก default constructor ของคลาสแม่มาใช้ ผลลัพธ์จะออกมาเป็นคลาสแม่ที่เป็น default และคลาสลูกที่เป็น defaultตามลำดับ และสุดท้ายถ้ามันสร้างจากคลาสลูกแบบมีพารามิเตอร์ผลลัพธ์จะออกมาเป็น คลาสแม่ที่มีพารามิเตอร์และ คลาสลูกที่มีพารมิเตอร์
Python
python เป็นภาษาที่มีความยืดหยุ่นสูง จึงมีการสืบทอดที่ง่ายกว่า C# กล่าวคือ มันสามารถเรียกใช้ constructor ของคลาสแม่โดย ใช้คีย์เวิร์ดว่า super() เช่นเดียวกับ java แต่pythonไม่ต้องระบุ data type แบบภาษา java C# และ C++ และการสืบทอดจะเขียนชื่อคลาสแม่ในวงเล็บของคลาสลูก class คลาสลูก (คลาสแม่): มีเมธอด __init__ สำหรับกำหนดค่าเริ่มต้นของ object หรือก็คือ constructor ของภาษา python ซึ่งแตกต่างจากภาษา java C# และ C++ ที่มีการสร้าง constructor โดยใช้ชื่อคลาสตัวเองเป็นชื่อเมธอด
example
output
จากตัวอย่างข้างต้น มีคลาส Emp เป็นคลาสแม่ และ คลาส Freelance เป็นคลาสลูก ซึ่ง Emp มีconstructor ที่รับพารามิเตอร์ id , name , Add เพื่อเก็บข้อมูลพนักงาน ในส่วนของ Freelance ซึ่งสืบทอดมาจาก Emp มีการใช้ super().__init__(id, name, Add) เพื่อเรียกใช้ constructor ของคลาสแม่ และเพิ่มคุณสมบัติใหม่ในคลาสตัวเองคือ Emails และมีการสร้าง object Emp_1 จากคลาส Freelance โดยส่งค่าไปยัง constructor ทั้งของแม่และลูก และพิมพ์แสดงผล id ,name ,address และ email ของ object ออกมา
Video Presentation
สไลด์นำเสนอ
Reference
C#
ส่วนเนื้อหา Inheritance in Constructors ของ c#:
GeeksforGeeks. (2023, Apr 06). C# | Inheritance in Constructors. GeeksforGeeks. https://www.geeksforgeeks.org/c-sharp-inheritance-in-constructors/
Microsoft. (2022, Feb 17). Inheritance - derive types to create more specialized behavior. Microsoft. https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
W3Schools. (n.d.). C# Inheritance. W3Schools. https://www.w3schools.com/cs/cs_inheritance.php
ส่วนตัวอย่างโค้ด ของ c#
Mohsen Bazmi. (2024, Mar 28). C# Constructor Inheritance. tutorials.eu. https://tutorials.eu/c-constructor-inheritance/
java
ส่วนของเนื้อหา Inheritance in Constructors ของ java:
W3Schools. (n.d.). C# | Java Inheritance. W3Schools.https://www.w3schools.com/java/java_inheritance.asp
Oracle. (n.d.). Inheritance. Oracle. https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
ส่วนตัวอย่างโค้ด ของ java
GeeksforGeeks. (2023, Dec 15). C# | Super Keyword in Java. GeeksforGeeks.https://www.geeksforgeeks.org/super-keyword/
c++
ส่วนของเนื้อหา Inheritance in Constructors ของ C++:
GeeksforGeeks. (2024, Oct 11). Inheritance in C++. GeeksforGeeks. https://www.geeksforgeeks.org/inheritance-in-c/
ส่วนตัวอย่างโค้ด ของ C++
Sruthy. (2024, Mar 10). C# | Inheritance In C++. softwaretestinghelp.https://www.softwaretestinghelp.com/inheritance-in-cpp/
python
ส่วนของเนื้อหา Inheritance in Constructors ของ python:
W3Schools. (n.d.). C# | Python Inheritance. W3Schools.https://www.w3schools.com/python/python_inheritance.asp
GeeksforGeeks. (2024, Jul 25). Inheritance in C++. GeeksforGeeks. https://www.geeksforgeeks.org/__init__-in-python/
ส่วนเนื้อหาและตัวอย่างโค้ด ของ java
GeeksforGeeks. (2024, Jul 7). C# | Python super(). GeeksforGeeks. https://www.geeksforgeeks.org/python-super/
Last updated

