Difference between Class and Structure

ณัฐชนน เที่ยงธรรม 650710540

ในC#ทั้ง class และ structure นั้นใช้ในการกำหนดcustom data typesเหมือนกัน,แต่ทั้งสองก็มีความต่างกันอยู่บ้าง เช่น

1)inheritanceหรือการสืบทอด

class สามารถสืบทอดมาจากclass อื่นได้ แต่ structure ไม่สามารถสืบทอดต่อกันได้

2)Reference type และ Value type

Class เป็นReference typeซึ่งหมายความว่าเมื่อสร้างobjectของclassการอ้างอิงของobjectนั้นจะถูกสร้างขึ้นและถ้าเกิดการเปลี่ยนแปลงเกี่ยวกับobjectนั้นจะทำให้เกิดการเปลี่ยนแปลงในทุกที่ๆถูกอ้างอิงถึงในทางกลับกันStructure จะเป็นแบบValue typeหมายความว่าเมื่อคุณสร้างตัวแปรของstructureค่าจริงของตัวแปรนั้นจะถูกเก็บไว้ในหน่วยความจำและการเปลี่ยนแปลงที่เกิดขึ้นกับตัวแปรจะไม่เปลี่ยนค่าของตัวแปรในหน่วยความจำ

3)Default Constructor

คลาสจะมี Default Constructorเสมอซึ่งจะถูกเรียกใช้เมื่อมีการสร้างobjectของclass ในทางตรงกันข้ามStructureจะไม่มีdefault constructorเพราะสามารถกำหนดconstructor เริ่มต้นได้

4)initializers

เมื่อสร้างobjectของคลาสตัวแปรจะถูกกำหนดเป็นค่าเริ่มต้น (null สำหรับ reference types และ 0 สำหรับvalue types)เมื่อคุณสร้างตัวแปรของstructureตัวแปรจะถูกกำหนดเป็นค่าเริ่มต้นเช่นกันแต่สามารถแก้ไขค่าเริ่มต้นได้

5)Size และ Performance

structureมักจะมีขนาดและความเร็วมากกว่าclassเพราะว่าไม่มีตัวแปรอ้างอิงหรือoverheadซึ่งทำให้structureสามารถส่งพารามิเตอร์ได้เร็วกว่าclass

ตัวอย่างการสร้างClass

using System;
// ประกาศสร้างคลาส
public class Author {
    //ชนิดข้อมูลภายในClass
    public string name;
    public string Book;
    
    //เมธอดของคลาส
    public void Details(string name, string Book)
    {
        this.name = name;
        this.Book = Book;
        Console.WriteLine("The name of the author is : " + name
                        + "\nMost poppular : " + Book
                        );
    }

    public static void Main(String[] args)
    {
 
        // สร้าง object
        Author obj = new Author();
        //เรียกใช้methodของclass
        obj.Details("Tony Stark", "อารมณ์ควบคุมคน");
    }
}
output
The name of the author is : Tony Stark
Most poppular : อารมณ์ควบคุมคน

ตัวอย่างการสร้างstructure

using System;
 
// ประกาศสร้างStructure
public struct Book
{
    public string Publisher;
    public string Author;
    public string Books;
}
 
class Seller{
 
    static void Main(string[] args)
    {
        // สร้างโดยไม่ต้องใช้คำสำคัญ"new"เหมือนคลาส
        Book b1;
        // กำหนดข้อมูลของb1
        b1.Publisher = "Siam inParis Publisher";
        b1.Author = "โอ้พระเจ้าGeorge RR Martínez";
        b1.Books = "A song of อ๊อด อ๊อด";
 
        // คำสั่งสำหรับแสดงผลการรัน
        Console.WriteLine("Publisher: " + b1.Publisher
                          + "\nAuthor: " + b1.Author
                           + "\nBook: " + b1.Books);
    }
}
output
Publisher: Siam inParis Publisher
Author: โอ้พระเจ้าGeorge RR Martínez
Book: A song of อ๊อด อ๊อด

Class และ Structure ใน Java

classในjavaคือแนวคิดพื้นฐานของการเขียนโปรแกรมเชิงวัตถุ(Object Oriented Programming , OOP)และยังเป็นแม่แบบในการสร้างobjectที่มีคุณสมบัติเดียวกัน

คุณสมบัติของclassในjava 1)classคือแม่แบบสำหรับการสร้างobject 2)classไม่มีการครอบครองพื้นที่ในหน่วยความจำ 3)classคือกลุ่มของตัวแปรที่มีชนิดข้อมูลแตกต่างกันและกลุ่มของmethod 4)classในjavaประกอบด้วย -Data memberหรือinstance -Method -Constructor -Nested Class -Interface

ตัวอย่างการสร้างclassในภาษาjava

class Student {
    int id;
    String name;
//constructor
Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public static void main(String args[])
    {
        // สร้างobject ของclass Student
        Student s1 = new Student(31,"antony");
        System.out.println(s1.id);
        System.out.println(s1.name);
    }
}

ผลลัพธ์ที่ได้คือ
31
antony

Javaนั้นไม่มีStructureเพราะสามารถใช้Classในทำงานได้เหมือนกันโดยการกำหนดinstanceให้เหมาะสม

Class และ Structure ใน Python

ในpython Classคือแม่แบบ(Blue print)สำหรับให้ผู้ใช้สร้างObjectบนพื้นฐานของการเขียนโปรแกรมเชิงวัตถุ(Object Oriented Programming)เหมือนในภาษาC#และJava

การสร้างClassในPython

class Student:
    student_id = 5

การสร้างObject

p1 = Student()

ฟังก์ชัน __init__()ในpythonทำอะไรได้บ่าง

โดยทุกคลาสจะมีFunctionที่เรียกว่า_init_()ซึ่งจะทำงานเมื่อมีการเรียกใช้Class ตัวอย่างเช่นสร้างClass Studentโดยให้ฟังก์ชั่น_init_()

class student:
    def __init__(self, name, age, sn):
        self.name = name
        self.age = age
        self.sn = sn

p1 = student("John", 16, 5)
print(p1.name)
print(p1.age)
print(p1.sn)
output
John
16
5

โดยเราสามารถกำหนดค่าของตัวแปรได้ในฟังก์ชั่น__init__()เลยดังตัวอย่าง

class student:
    def __init__(self, name, age, sn = 5):
        self.name = name
        self.age = age
        self.sn = sn

p1 = student("John", 16)
print(p1.name)
print(p1.age)
print(p1.sn)
output
John
16
5

ฟังก์ชัน __str__()ทำอะไรได้

โดยในpythonจะมีmethodที่ชื่อว่า__str__()มีไว้สำหรับกำหนดวิธีการแสดงผลในรูปแบบstringของobject

class student:
    def __init__(self, name, age, sn):
        self.name = name
        self.age = age
        self.sn = sn
	
    def __str__(self):
    	return f"{self.name}({self.age})"
    
p1 = student("John", 16, 5)
print(p1)

ตัวอย่างของการไม่ใช้__str__()จะทำให้outputที่ออกมาจะเป็นที่อยู่หน่วยความจำของออบเจกต์p1

class student:
    def __init__(self, name, age, sn):
        self.name = name
        self.age = age
        self.sn = sn

p1 = student("John", 16, 5)
print(p1)
เช่น
<__main__.Person object at 0xXXXXXXXX>

และclassสามารถมีmethodอยู่ข้างในได้ดังตัวอย่าง

class Student:
    def __init__(self, name, age, sn):
        self.name = name
        self.age = age
        self.sn = sn

    def myfunc(self):
        print("Hello, my name is " + self.name + ". I'm " + str(self.age) + " years old.")

p1 = Student("John", 16, 45)
p1.myfunc()
output
Hello, my name is John. I'm 16 years old.

และถ้าเราไม่อยากใช้คำว่าselfเราสามารถใช้คำอะไรก็ได้มาแทนselfโดยมีข้อแม้ว่าต้องเป็นparameterแรกของfunctionเช่นการใช้myobjectและabcแทน

class student:
  def __init__(myobject, name, age, sn):
    myobject.name = name
    myobject.age = age
    myobject.sn = sn

  def myfunc(abc):
    print("Hello, my name is " + abc.name + ". I'm " + str(abc.age) + " years old.")

p1 = student("John", 16, 45)
p1.myfunc()

และเหมือนJava Pythonก็ไม่มีstructureเหมือนกัน

แหล่งอ้างอิง

https://www.oracle.com/java/technologies/simple-familiar.html https://www.geeksforgeeks.org/difference-between-class-and-structure-in-c-sharp/ https://www.saladpuk.com/beginner-1/csharp101/intermediate/constructor https://www.geeksforgeeks.org/classes-objects-java/ https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/class https://www.geeksforgeeks.org/python-classes-and-objects/ https://www.w3schools.com/python/python_classes.asp https://www.geeksforgeeks.org/python-classes-and-objects/

Last updated