Class and Object

ญาณากร บุญสม 650710538

Class และ Object เป็นแนวคิดพื้นฐานในการเขียนโปรแกรมเชิงวัตถุ Object Oriented Programming (OOP)

CLASS คืออะไร

Class คือ ตัวต้นแบบหรือแม่แบบให้กับตัววัตถุ (Object) โดยที่จะกำหนดคุณสมบัติ(attributes) และพฤติกรรม (methods) ให้กับ object นั้นๆ

  • คุณสมบัติ (Attribute) คือ ตัวแปรใช้สำหรับเก็บข้อมูลต่างๆ เกี่ยวกับ Object

  • พฤติกรรม (Methods) คือ เป็นตัวกำหนดฟังก์ชันการทำงานของ Object

OBJECT คืออะไร

Object เปรียบเสมือนวัตถุชิ้นในโลกจริงๆที่มี สถานะ(State), พฤติกรรม (Behavior), เอกลักษณ์(Identity) ที่มี Class มาเป็นต้นแบบ

  • สถานะ (State) คือ สถานะหรือคุณสมบัติของตัว Object ที่กำหนดมาจาก Attributes

  • พฤติกรรม (Behavior) คือ ฟังก์ชันการทำงานของตัว Object ที่กำหนดมาจาก

    Methods

  • เอกลักษณ์ (Identity) คือ เอกลักษณ์ของตัว Object นั้นๆที่ทำให้แตกต่างจาก Object ตัวอื่นๆ แม้ว่าจะมี Attribute กับ ตัว Methods เหมือนกับ Object อื่นๆ ก็ตาม และช่วยให้สามารถตอบโต้กับตัว Object ตัวอื่นได้

ตัวอย่าง Class และ Object

Class ในที่นี้ คือ Class Human

มี Attributes คือ

  • Name

  • Age

  • Sex

  • Job

มี Method คือ

  • Walk

  • eat

  • sleep

OBJECT

Object จากตัวอย่างจะมี Steve, Bill, Larry ที่มี Class Human เป็นต้นแบบ

มี State คือ

  • Name

  • Sex

  • Age

  • Job

ซึ่งจะสังเกตได้ว่าในแต่ละ Object นั้นจะมี State หรือ Attribute ที่ต่างกันแม้จะได้ต้นแบบมาจาก Class Human เหมือนกัน

Obj Steve Obj Bill

Name = Steve Name = Bill

Sex = Male Sex = Male

Age = 56 Age = 60

Job = Unknown Job = Advisor

มี Method คือ

  • Walk

  • Eat

  • Sleep

ถึงแม้แต่ละ Object นั้นจะมี Attributes ที่ต่างกันแต่ทุก Object นั้นก็มีฟังก์ชันการทำงาน(Method) ที่เหมือนกับ Class Human

ตัวอย่าง Code Class and Object

class Human {
    String name;
    int age;
    String sex;
    String job;
    
    public Human(String name, int age, String sex, String job) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.job = job;
    }

    public void walk() {
        System.out.println(name + " is walking.");
    }

    public void eat() {
        System.out.println(name + " is eating.");
    }

    public void sleep() {
        System.out.println(name + " is sleeping.");
    }
}

สร้าง Class Human

สร้าง Constructor

กำหนด Attributes เป็น

  • String name

  • int age

  • String sex

  • String job

กำหนด Method เป็น

  • Walk

  • Eat

  • Sleep

public class Main {
    public static void main(String[] args) {
        Human person1 = new Human("Jane Doe", 24, "Female", "Engineer");
        person1.walk();
        person1.eat();
    
        Human person2 = new Human("Ceres Fauna", 20, "Female", "Vtuber");
        person2.walk();
        person2.eat();
    }
}

สร้าง Object person 1 จาก Class Human

ใส่ parameter เป็น "Jane Doe", 24, 'Female', "Engineer"

จากนั้นเรียกใช้ Method walk(), eat()

จากนั้นสร้าง Object person 2 จาก Class Human

ใส่ parameter เป็น "Ceres Fauna", 24, 'Female', "Vtuber"

จากนั้นเรียกใช้ Method walk(), eat()

จะเห็นได้ว่าถึงแม้ Attribute จะต่างกัน แต่ Jane doe และ Ceres Fauna นั้น มี Method หรือ ฟังก์ชันการทำงานเหมือนกัน

เปรียบเทียบระหว่าง Java C++ C# Python

class Human {
    String name;
    int age;
    String sex;
    String job;
    
    public Human(String name, int age, String sex, String job) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.job = job;
    }

    public void walk() {
        System.out.println(name + " is walking.");
    }

    public void eat() {
        System.out.println(name + " is eating.");
    }

    public void sleep() {
        System.out.println(name + " is sleeping.");
    }
}
public class Main {
    public static void main(String[] args) {
        Human person1 = new Human("Jane Doe", 24, "Female", "Engineer");
        person1.walk();
        person1.eat();
    
        Human person2 = new Human("Ceres Fauna", 20, "Female", "Vtuber");
        person2.walk();
        person2.eat();
    }
}

Video

Presentation

Class and Object Slide

Reference

C#: Class and Object (February 23, 2022) GeeksforGeeks : https://www.geeksforgeeks.org/c-sharp-class-and-object/

Classes and Objects in Java (September 03, 2024) GeeksforGeeks https://www.geeksforgeeks.org/classes-objects-java/

Python Classes and Objects (September 09, 2024) GeeksforGeeks https://www.geeksforgeeks.org/python-classes-and-objects/

Difference Between Object And Class (November 02, 2023) GeeksforGeeks https://www.geeksforgeeks.org/difference-between-class-and-object/

C++ Classes and Objects (October 11, 2024) GeeksforGeeks https://www.geeksforgeeks.org/c-classes-and-objects/

W3Schools C# W3Schools.com

https://www.w3schools.com/cs/cs_classes.php

W3Schools Java class W3Schools.com https://www.w3schools.com/java/java_classes.asp

W3Schools Python class W3Schools.com https://www.w3schools.com/python/python_classes.asp

W3Schools C++ class W3Schools.com https://www.w3schools.com/cpp/cpp_classes.asp

Last updated