INHERITANCE Multilevel Inheritanceสรยุทธ กรัณยพัฒนพงศ์ 650710586
Multilevel Inheritance คืออะไร?
Multilevel Inheritance เป็นการ Inheritance แบบหนึ่งที่สืบทอด Attribute, Method มาจากคลาสก่อนหน้า (เรียกว่า Base Class ก็ได้) และคลาสที่สืบทอดมาก็สืบทอดไปยังอีกคลาสอีกที (คลาสที่สืบทอดมาจะเรียก Derived Class) ซึ่ง คลาสลูกนั้นจะสามารถเข้าถึง Attribute, Method คลาสที่สืบทอดมาได้ ทำให้มีการจัดการที่มีประสิทธิภาพยิ่งขึ้นComment
ตัวอย่าง สร้างคลาสปู่ (Grandparent Class) สืบทอดไปยังคลาสพ่อ (Parent Class) จากคลาสพ่อไปคลาสลูก (Child Class) ดังรูป
ในที่นี้คลาส Grandparent จะเป็น Base Class ส่วนคลาส Parent จะสืบทอดมาจากคลาส Parent อีกที และคลาส Child ก็จะสืบทอดจากคลาส Parent มาอีกที
ตัวอย่างแรก โค้ดที่ใช้ Multilevel Inheritance ในภาษา C#
Copy using System;
class Grandfather {
public void pray() {
Console.WriteLine("Praying.");
}
}
class Father : Grandfather {
public void work() {
Console.WriteLine("Working.");
}
}
class Son : Father {
public void play() {
Console.WriteLine("Playing.");
}
}
class Family {
static void Main(string[] args) {
Son objSon = new Son();
objSon.pray();
objSon.work();
objSon.play();
}
}
Copy Praying.
Working.
Playing.
จากโค้ด จะมีคลาส Grandfather ที่มีเมธอด pray()
เราจะเห็นได้ว่ามีการใช้ Inheritance ในคลาส Father (สืบทอดจากคลาส Grandfather) มีการสร้างเมธอด work()
และ คลาส Son (สืบทอดจากคลาส Father) มีการสร้างเมธอด Energy()
คลาส Son นั้นจะสามารถใช้เมธอด pray() และ work()
ได้เพราะสืบทอดมาจากคลาส Father ส่วนคลาส Father จะสามารถใช้เมธอด pay()
ได้ แต่ คลาส Grandfather ไม่สามารถใช้เมธอดอื่นได้นอกจากในคลาส Grandfather เอง
ตัวอย่างที่ 2 โค้ดที่ใช้ Multilevel Inheritance ในภาษา C#
Copy using System;
class Vehicle
{
private string brand;
private int speed;
public Vehicle(string brand, int speed)
{
this.brand = brand;
this.speed = speed;
}
public void DisplayInfo()
{
Console.WriteLine($"Brand: {brand}");
Console.WriteLine($"Speed: {speed} km/h");
}
}
class Car : Vehicle
{
private int numberOfDoors;
public Car(string brand, int speed, int numberOfDoors)
: base(brand, speed)
{
this.numberOfDoors = numberOfDoors;
}
public void DisplayNumberOfDoors()
{
Console.WriteLine($"Doors: {numberOfDoors}");
}
}
class ElectricCar : Car
{
private int batteryCapacity;
public ElectricCar(string brand, int speed,
int numberOfDoors, int batteryCapacity)
: base(brand, speed, numberOfDoors)
{
this.batteryCapacity = batteryCapacity;
}
public void DisplayBatteryCapacity()
{
Console.WriteLine($"Battery Capacity: {batteryCapacity} kWh");
}
}
public class SuperCarCare
{
public static void Main(string[] args)
{
Vehicle Fino = new Vehicle("Fino",80);
Fino.DisplayInfo();
Console.WriteLine();
Car Alphard = new Car("Alphard",200,5);
Alphard.DisplayInfo();
Alphard.DisplayNumberOfDoors();
Console.WriteLine();
ElectricCar Gte = new ElectricCar("Drago Gte",260,4,90);
Gte.DisplayInfo();
Gte.DisplayNumberOfDoors();
Gte.DisplayBatteryCapacity();
}
}
Copy Brand: Fino
Speed: 80 km/h
Brand: Alphard
Speed: 200 km/h
Doors: 5
Brand: Drago Gte
Speed: 260 km/h
Doors: 4
Battery Capacity: 90 kWh
ในตัวอย่างนี้เราได้มีการใช้ Constructor ด้วยซึ่งการสืบทอดต้องส่งสมาชิกไปหาคลาสแม่ ด้วย ซึ่งในภาษา C# จะใช้ base
ในการส่ง ดูได้จากคลาส Car มีการส่งพารามิเตอร์ brand กับ speed ไปหาคลาสแม่อย่าง Vehicle ส่วนคลาส ElectricCar ส่งพารามิเตอร์ brand, speed และ numberOfDoors ไปคลาสที่สืบทอดมาอีกทีนั่นเอง
ส่วนในเมธอด Main()
นั้นมีการสร้าง Object มา 3 ตัวคือ Fino, Alphard และ Gte ซึ่งแต่ละ Object จะเข้าถึงเมธอดที่ตัวคลาสสืบทอดมาได้เท่านั้น คือ Gte สามารถเข้าถึงเมธอดคลาสได้ทั้งหมด เพราะสืบทอดมาจากคลาส Car (ซึ่ง Car ก็สืบทอดมาจาก Vehicle) Alphard เข้าถึงเมธอดคลาส Car กับคลาส Vehicle ส่วน Fino เข้าถึงได้แค่ในคลาสตัวเองอย่าง Vehicle เท่านั้น
ข้อดี-ข้อเสีย ของการใช้ Multilevel Inheritance
ข้อดี
การจัดการที่ดี มีการแยกโค้ดเป็นลำดับชั้นๆ
การใช้ซ้ำ สามารถนำคุณสมบัติจากคลาสที่อยู่สูงกว่ามาใช้ซ้ำได้
ข้อเสีย
แก้ไขได้ยาก ถ้ามีการสืบทอดหลายชั้น จะทำให้สับสน อาจเกิดการ error เป็น Domino ได้
เปรียบเทียบ Multi Level Inheritance ภาษา C# กับ ภาษาอื่นๆ
Copy using System;
class Social {
private string accountName;
public Social(string accountName) {
this.accountName = accountName;
}
public void DisplayAccountName() {
Console.WriteLine("Account Name: " + accountName);
}
}
class YouTube : Social {
private int subscribers;
public YouTube(string accountName, int subscribers) : base(accountName) {
this.subscribers = subscribers;
}
public void DisplaySubscribers() {
Console.WriteLine("Subscribers: " + subscribers);
}
}
class TikTok : YouTube {
private int mostShortViewed;
public TikTok(string accountName, int subscribers, int mostShortViewed)
: base(accountName, subscribers) {
this.mostShortViewed = mostShortViewed;
}
public void DisplayMostVideoViewed() {
Console.WriteLine("Most Short Viewed: " + mostShortViewed);
}
}
public class MultiLevelInheritance {
public static void Main(string[] args) {
TikTok tikTok = new TikTok("PunchRakMaew", 2600000, 4200000);
tikTok.DisplayAccountName();
tikTok.DisplaySubscribers();
tikTok.DisplayMostVideoViewed();
}
}
Copy class Social {
private String accountName;
public Social(String accountName) {
this.accountName = accountName;
}
public void displayAccountName() {
System.out.println("Account Name: " + accountName);
}
}
class YouTube extends Social {
private int subscribers;
public YouTube(String accountName, int subscribers) {
super(accountName);
this.subscribers = subscribers;
}
public void displaySubscribers() {
System.out.println("Subscribers: " + subscribers);
}
}
class TikTok extends YouTube {
private int mostShortViewed;
public TikTok(String accountName, int subscribers, int mostShortViewed) {
super(accountName, subscribers);
this.mostShortViewed = mostShortViewed;
}
public void displayMostVideoViewed() {
System.out.println("Most Short Viewed: " + mostShortViewed);
}
}
public class MultiLevelInheritance {
public static void main(String[] args) {
TikTok tikTok = new TikTok("PunchRakMaew", 2600000, 4200000);
tikTok.displayAccountName();
tikTok.displaySubscribers();
tikTok.displayMostVideoViewed();
}
}
Copy #include <iostream>
#include <string>
using namespace std;
class Social {
private:
string accountName;
public:
Social(string accountName) {
this->accountName = accountName;
}
void DisplayAccountName() {
cout << "Account Name: " << accountName << endl;
}
};
class YouTube : public Social {
private:
int subscribers;
public:
YouTube(string accountName, int subscribers) : Social(accountName) {
this->subscribers = subscribers;
}
void DisplaySubscribers() {
cout << "Subscribers: " << subscribers << endl;
}
};
class TikTok : public YouTube {
private:
int mostShortViewed;
public:
TikTok(string accountName, int subscribers, int mostShortViewed)
: YouTube(accountName, subscribers) {
this->mostShortViewed = mostShortViewed;
}
void DisplayMostVideoViewed() {
cout << "Most Short Viewed: " << mostShortViewed << endl;
}
};
void main() {
TikTok tikTok("PunchRakMaew", 2600000, 4200000);
tikTok.DisplayAccountName();
tikTok.DisplaySubscribers();
tikTok.DisplayMostVideoViewed();
}
Copy #include <stdio.h>
#include <string.h>
struct Social {
char accountName[50];
};
void displayAccountName(struct Social social) {
printf("Account Name: %s\n", social.accountName);
}
struct YouTube {
struct Social social;
int subscribers;
};
void displaySubscribers(struct YouTube youtube) {
printf("Subscribers: %d\n", youtube.subscribers);
}
struct TikTok {
struct YouTube youtube;
int mostShortViewed;
};
void displayMostVideoViewed(struct TikTok tikTok) {
printf("Most Short Viewed: %d\n", tikTok.mostShortViewed);
}
void main() {
struct TikTok tikTok;
strcpy(tikTok.youtube.social.accountName, "PunchRakMaew");
tikTok.youtube.subscribers = 2600000;
tikTok.mostShortViewed = 4200000;
displayAccountName(tikTok.youtube.social);
displaySubscribers(tikTok.youtube);
displayMostVideoViewed(tikTok);
}
Copy class Social:
def __init__(self, account_name):
self.account_name = account_name
def display_account_name(self):
print("Account Name:", self.account_name)
class YouTube(Social):
def __init__(self, account_name, subscribers):
super().__init__(account_name)
self.subscribers = subscribers
def display_subscribers(self):
print("Subscribers:", self.subscribers)
class TikTok(YouTube):
def __init__(self, account_name, subscribers, most_short_viewed):
super().__init__(account_name, subscribers)
self.most_short_viewed = most_short_viewed
def display_most_video_viewed(self):
print("Most Short Viewed:", self.most_short_viewed)
tik_tok = TikTok("PunchRakMaew", 2600000, 4200000)
tik_tok.display_account_name()
tik_tok.display_subscribers()
tik_tok.display_most_video_viewed()
Copy Account Name: PunchRakMaew
Subscribers: 2600000
Most Short Viewed: 4200000
จากการเปรียบเทียบจะเห็นได้ว่า โครงสร้างโค้ด Multilevel Inheritance แต่ละภาษาไม่ได้ต่างกันมากนัก มีภาษา C ที่จะไม่มี Class ให้ใช้ จะใช้เป็น Struct
แทนเพื่อเลียนแบบการใช้ Class ในภาษาอื่นๆ และเพื่อจำลองแบบ Inheritance เราจึงใช้การ Composition แทน
Slide Presentation
Video Presentation
อ้างอิง
Sukhpinder Singh, C# .Net. (2023, February 17). Mastering Multi-Level inheritance in C#: Building scalable and reusable code hierarchies | .Net Programming. Medium . https://medium.com/c-sharp-programming/mastering-inheritance-in-c-multi-level-inheritance-3993e8ed1833
-> เนื้อหาส่วนที่เป็น ข้อดีข้อเสียของการใช้ Multilevel Inheritance
C# Inheritance - javatpoint . (n.d.). https://www.javatpoint.com/c-sharp-inheritance
-> เนื้อหาส่วนที่เป็น ความหมายของการใช้ Multilevel Inheritance ใน C#
GeeksforGeeks. (2023, April 6). C# | Multilevel Inheritance . GeeksforGeeks. https://www.geeksforgeeks.org/c-sharp-multilevel-inheritance/
-> เนื้อหาส่วนที่เป็น ความหมายของการใช้ Multilevel Inheritance ใน C#
C# example for MultiLevel inheritance . (n.d.). https://www.tutorialspoint.com/chash-example-for-multilevel-inheritance
-> นำเนื้อหาส่วนที่เป็น Syntax การเขียน Multilevel Inheritance เบื้องต้นใน C#
Multilevel inheritance in Java . (n.d.). https://www.tutorialspoint.com/multilevel-inheritance-in-java
-> นำเนื้อหาส่วนที่เป็น Syntax การเขียน Multilevel Inheritance ใน Java
GeeksforGeeks. (2024, February 23). Multilevel inheritance in Python . GeeksforGeeks. https://www.geeksforgeeks.org/multilevel-inheritance-in-python/
-> นำเนื้อหาส่วนที่เป็น Syntax การเขียน Multilevel Inheritance ใน Python
W3Schools.com . (n.d.-b). https://www.w3schools.com/cpp/cpp_inheritance_multilevel.asp
-> นำเนื้อหาส่วนที่เป็น Syntax การเขียน Multilevel Inheritance ใน C++
Last updated 7 months ago