Flutter EP.2.1 Class และ Object ในภาษา Dart

7 Jun 2020,
Share: 
Cover image

สวัสดีครับ หลังจาก EP.2 เราได้เรียนรู้ Syntax ของภาษา Dart กันไปแล้ว สําหรับ EP นี้ก็จะเป็น เนื้อหาเพิ่มเติมซึ่งจะเกี่ยวข้องกับ Class และ Object ในภาษา Dart

Class and Object

การสร้าง class และ object ใน Dart ก็จะคล้ายๆกับภาษาอื่นๆในโลกนี้ ไม่มีอะไรแตกต่างกันมาก

class People {
    int id;
    String name;

    void hello(){
        print('Hi, my name is $name');
    }
}

ส่วนการสร้าง Object ก็จะทําได้สองแบบคือ

var people = new People();  // จะใส่ new ก็ได้
var people = People();      // ไม่ต้องใส่ new ก็ได้

people.name = 'Thiti';
people.hello();

Private vs Public

ในภาษา Dart จะไม่มี Keyword ที่ใช้ระบุว่า Field ไหน หรือ Method ไหนเป็น Private หรือ Public แต่จะใช้ _ (underscore) นําหน้า Field หรือ Method ที่ต้องการจะให้เป็น Private ดังตัวอย่างนี้

class MyClass {
    int _privateField;
    int publicField;

    void _privateMethod() => 0
    void publicMethod() => 0
}

Getter Setter fields

ใน OOP ยุคเก่า การใช้งาน Getter หรือ Setter ก็จะสร้างตัวแปร Field ที่เป็น Private แล้วสร้าง Method Getter และ Setter มาครอบอีกที

แต่ในภาษา Dart ก็มี Getter และ Setter ให้ใช้ได้เลย ซึ่งจะสามารถใช้งานได้ตามตัวอย่างด้านล่างครับ

class People {
    String _name;

    // Getter
    int get name => _name;

    // Setter
    void set name(String name) => _name = name;

    // หรือแบบย่อ คือไม่ต้องใส่ void (Recommend)
    set name(String name) => _name = name;
}

Constructor

Constructor คือการกําหนดค่าเริ่มต้นครั้งแรกให้กับ Class ก็เหมือนๆกับภาษาอื่นๆทั่วไปครับ

class People {
    int _id;
    String _name;

    People(int id, String name){
        _id = id;
        _name = name;
    }
}

var people = People(1, 'Thiti');

หรือประกาศเป็นแบบ Named Parameter ก็ได้เช่นกัน

class People {
    int id;
    String name;

    People({this.id, this.name});
}

var people = People(id: 1, name: 'Thiti');

Named constructors

ภาษา Dart ไม่สามารถทำ Method Overloading หรือการสร้าง Method ชื่อเดียวกันรับ Parameter หลายแบบ จะไม่สามารถทําได้

แต่ Dart สามารถสร้าง Constructors หลายๆตัวได้ ซึ่งมีไว้เพื่อสร้าง Object หลายๆแบบ โดยเรียกว่า Named constructors ตัวอย่างตามนี้ครับ

class Point {
    int x, y;

    Point(this.x, this.y);

    Point.origin() {
        x = 0;
        y = 0;
    }
}

var point1 = Point(10, 20);
var point0 = Point.origin();

Factory constructors

นอกจาก Named constructors ภาษา Dart ก็ยังมี Factory constructors ให้ใช้อีก Factory constructors นั้นทำงานเหมือนกับ Constructor ทุกอย่าง แต่จะแตกต่างกันตรงที่ Constructor จะกําหนดค่าให้กับตัวเอง เนื่องจาก Constructor ถือเป็นเมธอดตัวหนึ่งของ object อยู่แล้ว แต่ว่า Factory constructors จะเป็นการสร้าง object ชิ้นใหม่ขึ้นมาเลย ดังนั้นจะไม่สามารถเรีกใช้ this เพื่อกําหนดค่าให้กับ Field ได้ตรงๆ ลองดูตัวอย่างครับ

class Profile{
    static Profile _instance;
    String name;

    Profile({this.name});

    factory Profile.fac1() => _instance ??= Profile(name: "Default name");
}

void main() {
  var profile1 = Profile.fac1();

  profile1.name = "Thiti";

  var profile2 = Profile.fac1();


  print('profile1: ${profile1.name}'); // profile1: Thiti
  print('profile2: ${profile2.name}'); // profile2: Thiti
}

เนื้อหาในบทความนี้ก็มีอยู่เท่านี้ครับ แล้วพบกัน EP หน้านะครับ ขอบคุณครับ

Suggestion blogs

ESP8266 ควบคุม i/o ผ่าน web (Access point)

ในบทความนี้ผมจะอธิบายถึงวิธีการทําให้ ESP8266 เป็น Access point ให้อุปกรณ์อุปกรณ์อื่นๆ ที่เชื่อมต่อ wifi ได้ เช่น computer, smart phone ฯลฯ มาเชื่อมต่อกับ SEP8266 แล้วควบคุม i/o ปิด/เปิด LED ผ่านเว็บ ด้วยวิธีส่งข้อมูลผ่าน HTTP_GET เช่น

BMS (Battery Management System) คืออะไร

BMS ย่อมาจาก Battery Management System หรือถ้าแปลเป็นไทยก็คือ ระบบจัดการแบตเตอรี่ ซึ่งจะทําหน้าที่ในการ Maintain balance cell battery เพื่ออัดไฟให้เต็มมากที่สุด และยืดอายุการใช้งานแบตเตอรี่ หากยังนึกภาพไม่ออก ผมจะยกตัวอย่างดังนี้ครับ เมื่อเรานําแบตเตอรี่หลายๆก้อนมาต่อกันแบบอนุกรมเพื่อให้ได้แรงดันไฟฟ้าตามที่เราต้องการแบบในรูป

Ascii คือ อะไร?

Ascii คือ อะไร?ในระบบคอมพิวเตอร์ จะใช้เลขฐานสอง(0, 1) ในการทํางาน เลขฐานสอง 1 ตัว เราจะเรียกว่า 1 bit สามารถแทนสถานะได้ 2 สถานะ คือ 0 กับ 1 ถ้าเรานําเลขฐานสองมาร่วมกัน 8 ตัว(8 bit) เราจะเรียกว่า 1 Byte สามารถใช้แทนสถานะได้ถึง 256 สถานะ(2 ยกกําลัง 8) Ascii เป็นการกําหนดมาตรฐานเพื่อใช้ในระบบคอมพิวเตอร์ และอุกปรณ์ Digitall ต่างๆ โดยนําเลขฐานขนาด 1 Byte มาแทนตัว


Copyright © 2019 - 2025 thiti.dev |  v1.51.0 |  Privacy policy | 

Build with ❤️ and Astro.

Github profile   Linkedin profile   Instagram   X profile   Nostr   Youtube channel   Telegram   Email contact   วงแหวนเว็บ