Multiple inheritance using interfaces

สิรภัทร ลีลาจิตต์เจริญ 650710587

multiple inheritance

หมายถึง การที่คลาสหนึ่งสืบทอดคุณสมบัติจากคลาสหลายๆคลาสในเวลาเดียวกัน แต่ในภาษา C# นั่นไม่สามารถให้คลาสหลายคลาสสืบทอดให้คลาสๆหนึ่งได้โดยตรงได้โดยตรง จึงต้องใช้ interfaces มาสืบทอดให้แทน เพราะงั้นถึงต้องใช้ multiple inheritance using interfaces ในภาษา C#

multiple inheritance using interfaces

หมายถึง การให้คลาสหนึ่งสามารถรับการสืบทอดจากหลายๆ interfaces ได้ในเวลาเดียวกัน ใน interfaces นั่นจะมีชุด method ที่ยังไม่มีการ implement อยู่ และคลาสที่รับการสืบทอดจาก interfaces เหล่านั้นจะต้องทำการ implement method เหล่านั้นให้ชัดเจน

ข้อดี

  1. ความยืดหยุ่นในการใช้: คลาสได้รับการอนุญาตให้สืบทอดได้จากหลาย interfaces ทำให้สามารถเพิ่มคุณสมบัติให้กับคลาสได้ยืดหยุ่นและไม่ต้องสืบทอดการใช้งานจากคลาสหลัก

  2. การนำโค้ดมาใช้ซ้ำ: คลาสที่สืบทอด interfaces มานั้นสามารถนำโค้ดจาก interfaces ต่างๆมาใช้ซ้ำได้ ทำให้โค้ดดูแลได้ง่ายและแก้ไขง่ายขึ้น

  3. โค้ดอ่านง่าย: หากเข้าใจหลักการ multiple inheritance using interfaces ก็จะทำให้การออกแบบโค้ดอ่านง่ายและง่ายต่อการเข้าใจ

ข้อเสีย

  1. การตั้งชื่อที่ขัดแย้งกัน: การสืบทอดจากหลาย interfaces อาจทำให้เกิดความผิดพลาดในการตั้งชื่อได้หาก interfaces ที่มากกว่าสองตัวขึ้นไปมีชื่อเดียวกัน แต่สามารถมี interfaces ชื่อเดียวกันใน namespace ที่ต่างกัน

  2. ความซับซ้อน: หาก interfaces มีหลายตัวมากเกินไป หรือไม่เข้าใจหลักการ multiple inheritance using interfaces อาจทำให้โค้ดมีความซับซ้อนและยากต่อการเข้าใจ

นี้คือภาพตัวอย่างการทำงานของ multiple inheritance using interfaces ในภาษา C#

จากในรูป multiple inheritance using interfaces นั้น interfaces จะต้องมีมากกว่า 2 ตัวขึ้นไป โดยข้างใน interfaces จะมี method ที่ยังไม่ทำการ implement อยู่ใน interfaces จากนั้น interfaces จะทำการสืบทอดไปยัง class ที่เรียกใช้งาน interfaces เหล่านั้น และคลาสจะทำการ implement method ที่ได้รับสืบทอดมาจาก interfaces

มาลองดูตัวอย่างการใช้งานของ multiple inheritance using interfaces ในภาษา C#

using System;

interface IShape
{
	double GetArea();
}

interface IColor
{
	string GetColor();
}

class Rectangle : IShape, IColor
{
	private double length;
	private double breadth;
	private string color;

	public Rectangle(double length, double breadth, string color)
	{
		this.length = length;
		this.breadth = breadth;
		this.color = color;
	}

	public double GetArea()
	{
		return length * breadth;
	}

	public string GetColor()
	{
		return color;
	}
}

class Program
{
	static void Main(string[] args)
	{
		Rectangle rect = new Rectangle(5, 10, "blue");
		Console.WriteLine("Area of rectangle: " + rect.GetArea());
		Console.WriteLine("Color of rectangle: " + rect.GetColor());
	}
}

Output

Area of rectangle : 50
Color of rectangle : blue

ในตัวอย่าง เรามีสอง interfaces คือ IShape และ IColor ซึ่งคลาส Rectangle นำ interfaces IShape และ IColor ไปใช้งานซึ่งจะกำหนดการทำงานต่างๆไว้ในคลาส Rectangle โดยการทำงานนั้นจะเพิ่มค่า length,breadth เป็นค่า double และ color เป็น string และมี method 2 ตัว ชื่อ GetArea() และ GetColor() ซึ่ง GetArea() จะทำการเอาค่า length มาคูณกับค่า breadth และ return ค่าที่คูณได้ออกมา ส่วน GetColor() จะทำการ return ค่าcolor ออกมา ส่วนคลาส Program สร้าง instance ของคลาส Rectangle โดยกำหนดค่า length = 5 ,breadth = 10 และ color = "blue" และทำการเรียกใช้ method GetArea() และ GetColor()

ลองนำตัวอย่างของ multiple inheritance using interfaces ในภาษา C# มาเทียบกับ multiple inheritance using interfaces ของภาษา C,Java,Python ว่ามีการทำงานที่แตกต่างกันอย่างไร

เทียบกับในภาษา C

#include <stdio.h>

struct Rectangle {
    double length;
    double breadth;
    char color[20];
};

double GetArea(struct Rectangle rect) {
    return rect.length * rect.breadth;
}

void GetColor(struct Rectangle rect, char* color) {
    sprintf(color, "%s", rect.color);
}

int main() {
    struct Rectangle rect = {5, 10, "blue"};
    char color[20];

    printf("Area of rectangle: %.2f\n", GetArea(rect));
    GetColor(rect, color);
    printf("Color of rectangle: %s\n", color);
    
    return 0;
}

ข้อแตกต่างของภาษา C กับภาษา C#

ความแตกต่างระหว่างภาษา C# และ C คือ เนื่องจาก C ไม่ได้เป็นโปรแกรมเชิงวัตถุ จึงต้องใช้ struct แทนคลาส และไม่มีการสืบทอดหรือ interface และฟังก์ชันจะถูกเขียนแยกต่างหากและต้องผ่านโครงสร้างเพื่อทำงาน ดังนั้นการนำภาษา C มาทำงานด้านโปรแกรมเชิงวัตถุจึงไม่เหมาะเท่าไหร่

เทียบกับในภาษา Java

interface Shape {
    double getArea();
}

interface Color {
    String getColor();
}

class Rectangle implements Shape, Color {
    private double length;
    private double breadth;
    private String color;

    public Rectangle(double length, double breadth, String color) {
        this.length = length;
        this.breadth = breadth;
        this.color = color;
    }

    public double getArea() {
        return length * breadth;
    }

    public String getColor() {
        return color;
    }

    public static void main(String[] args) {
        Rectangle rect = new Rectangle(5, 10, "blue");
        System.out.println("Area of rectangle: " + rect.getArea());
        System.out.println("Color of rectangle: " + rect.getColor());
    }
}

ข้อแตกต่างของภาษา Java กับภาษา C#

Java มีความคล้ายคลึงกับภาษา C# เนื่องจากเป็นประเภท OOP เหมือนกัน โดยแทบจะเหมือนทุกอย่าง โดยความแตกต่างของ 2 ภาษานี้ก็คือ Java ต้องใช้คำว่า implement แทนการใช้ colon(:) และ Java ต้องใช้ public และ static ในการเข้าถึง method ดังนั้นภาษา Java จึงเหมาะกับการทำ multiple inheritance using interfaces

เทียบกับในภาษา Python

class Rectangle:
    def __init__(self, length, breadth, color):
        self.length = length
        self.breadth = breadth
        self.color = color

    def get_area(self):
        return self.length * self.breadth

    def get_color(self):
        return self.color

rect = Rectangle(5, 10, "blue")
print("Area of rectangle:", rect.get_area())
print("Color of rectangle:", rect.get_color())

ข้อแตกต่างของภาษา Python กับภาษา C#

ความแตกต่างระหว่างภาษา C# และ Python คือ Python ไม่จำเป็นจะต้องระบุชนิดข้อมูลหรือประกาศ interface และใช้ self ในการเข้าถึงคุณสมบัติของคลาส ซึ่งทำให้โค้ดในภาษา Python มีโค้ดที่เรียบง่าย สั้น และยืดหยุ่นกว่ามาก

Presentation:

Video:

Video Presentation

Reference:

GeeksforGeeks. (2023). C# | Multiple inheritance using interfaces. Retrieved from GeeksforGeeks:https://www.geeksforgeeks.org/c-sharp-multiple-inheritance-using-interfaces/

dotnettutorials. (2021). Multiple Inheritance in C# with Examples. Retrieved from dotnettutorials:

https://dotnettutorials.net/lesson/multiple-inheritance-csharp/

expert-programming-tutor(2023). Multiple Inheritance คืออะไร?. Retrieved from expert-programming-tutor: https://expert-programming-tutor.com/tutorial/article/KC0020111004_what_is_multiple_inheritance_in_OOP_concept_in_C++_language.php

Last updated