Method returning an object

นิธิ มีแสงเพ็ชร์ 650710559

Method returning an object : Method ที่คืนค่าเป็น object ของ class เช่น object ที่เราสร้างขึ้นเอง

ตัวอย่าง: ถ้าเรามี class ชื่อ Animal ไว้อยู่ method ที่คืนค่า object Animal จะทำหน้าที่สร้างและส่งคืน object Animal ให้กับผู้เรียกใช้งาน

Method นี้สามารถคืนค่าได้หลายประเภท : Object เช่น Animal, ข้อมูลพื้นฐาน เช่น int, string, bool

แต่ถ้าพูดถึง returning an object เราจะเน้นที่การคืนค่า object เป็นหลัก

ตัวอย่างของการใช้ Method returning an object ใน C#

using System;

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }

    public Book(string title, string author)
    {
        Title = title;
        Author = author;
    }
}

public class Library
{
    // ใช้สำหรับสร้างและคืนค่าวัตถุ Book ที่เป็น object
    public Book CreateBook(string title, string author)
    {
        // สร้าง object Book และ return object ไปยังที่เรียกใช้งาน
        return new Book(title, author);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Library library = new Library();

        // เรียกใช้ Method CreateBook ที่จะคืนค่าวัตถุ Book
        Book book1 = library.CreateBook("Harry Potter", "J. K. Rowling");
        Book book2 = library.CreateBook("Sherlock Holmes", "Sir Arthur Conan Doyle");

        // แสดงผลข้อมูลของหนังสือที่ได้จากการเรียก Method ที่คืนค่าเป็นวัตถุ Book
        Console.WriteLine($"Title: {book1.Title}, Author: {book1.Author}");
        Console.WriteLine($"Title: {book2.Title}, Author: {book2.Author}");
    }
}
  • จาก code ตัวอย่างจะมี class Book ที่เก็บ 2 ตัวแปรคือ title, author

  • ใน class Library จะมีพระเอกของเราคือ CreateBook ที่เป็น Method return object รับ parameter สองตัวคือ title, author เมื่อเราเรียกใช้ method นี้ มันจะสร้าง object Book ขึ้นมาแล้ว return ไปยังที่ถูกเรียกใช้

  • ใน Main สร้าง object Library แล้วเรียกใช้ library.CreateBook เพื่อสร้าง object Book แล้วเก็บไว้ใน book1, book2 แล้วก็พิมพ์ผลลัพธ์ออกมา

Output

Title: Harry Potter, Author: J. K. Rowling
Title: Sherlock Holmes, Author: Sir Arthur Conan Doyle

ผลลัพธ์ก็จะพิมพ์ object Book ที่เก็บไว้ใน book1, book2 ที่เก็บตัวแปร title, author ออกมา

เปรียบเทียบกับ Java, C, Python

  • ใน Java เราจะต้องมีตัว method getTitle() และ getAuthor() เพราะข้อกำหนดเรื่องการห่อหุ่ม เพื่อเข้าถึงตัว title, author ไม่เหมือนกับ C# ที่สามารถเข้าถึงได้โดยตรงโดยใช้ get, set แล้วที่เหลือก็คล้ายใน C#

  • เนื่องจาก ภาษา C ไม่ใช่ภาษา OOP จึงไม่มี object เราจะใช้ struct เข้ามาใช้แทน ซึ่งจะมีความคล้ายๆ กับ method ใน OOP โดยมี struct Book, struct createBook ที่คืนโครงสร้าง Book แล้วก็มี strcpy ไว้คัดลอกค่า string จากค่าหนึ่งไปอีกค่าหนึ่ง เช่น ค่า A คัดลอกส่งไปยังค่า B

  • ใน Python จะไม่ค่อยมีความซับซ้อนอะไรมาก มี method __init__ เป็น constructor แล้วก็สร้าง create_book ไว้ใน class Library เหมือนกับในภาษา C# ส่วนที่เหลือก็คล้ายในC#

class Book {
    private String title;
    private String author;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
    // get title
    public String getTitle() {
        return title;
    }
    // get author
    public String getAuthor() {
        return author;
    }
}

class Library {
    //method return object
    public Book createBook(String title, String author) {
        return new Book(title, author);
    }
}

public class Main {
    public static void main(String[] args) {
        Library library = new Library();

        Book book1 = library.createBook("Harry Potter", "J. K. Rowling");
        Book book2 = library.createBook("Sherlock Holmes", "Sir Arthur Conan Doyle");

        System.out.println("Title: " + book1.getTitle() + ", Author: " + book1.getAuthor());
        System.out.println("Title: " + book2.getTitle() + ", Author: " + book2.getAuthor());
    }
}

Output

Title: Harry Potter, Author: J. K. Rowling
Title: Sherlock Holmes, Author: Sir Arthur Conan Doyle

จากผลลัพธ์เห็นได้ว่า ทุกภาษาสามารถมีผลลัพธ์เหมือนกันได้แต่อาจมีการเขียน code หรือวิธีการที่ไม่เหมือนกัน

Video

Slide

Reference

ความหมายและการใช้ Method returning object ใน C#

ดูตัวอย่างเพิ่มเติมเรื่อง code Method returning object ใน C#

การใช้และตัวอย่างของ Method returning object ใน java

การใช้ Struct แทนใน การ return ค่าใน C

ดูการใช้ Struct แทนใน การ return ค่าใน C เพิ่มเติม

การจัดการกับค่า string ใน C

Method return object ใน Python

ดูตัวอย่าง Code ใน Python

Last updated