Cover image

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

7 Jun 2020

Share to:

สวัสดีครับ หลังจาก 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

วิธีการโฮสต์ไฟล์ IPFS ให้ออนไลน์ตลอดไป

สวัสดีครับ โดยปกติเมื่อ Add File เข้า IPFS แล้ว IPFS จะทําการ Pin File ให้โดยอัตโนมัติ หมายความว่า File นั้นจะยัง Online อยู่ ใน IPFS Network แต่เมื่อใดเราปิดเครื่อง หรือปิด IPFS Daemon แล้ว File นั้นจะถูกลบออกจาก IPFS Network ในเวลาต่อมา (ประมาณ 24 ชั่วโมง) ดังนั้นถ้าต้องการจะให้ File คงอยู่ตในลอดไป จะต้องให้เครื่องคอมพิวเตอร์ หรือ Node อื่นๆ Pin File ของเราไว้ในระบบด้วย ยิ่งมี Node ที่ Pin File ของเราเยอะ File ของเรานั้นก็จะอยู่ในระบบ ไม่หายไปไหน ถึงแม้ว่าเราปิดเครื่องของเราแล้วก็ตาม

สร้าง Chrome Extension ด้วย Vuejs

ในการสร้าง Chrome Extension หรือเรียกเป็นภาษาไทย "ส่วนขยาย" จะใช้ HTML/CSS/JavaScript ในการเขียน ซึ่งจะคล้ายกับการเขียนเว็บทั่วๆไป ในเมื่อมันเป็น HTML/CSS/JavaScript ก็จับมาเขียนด้วย Vuejs ซะเลย เพื่อให้ง่ายต่อการพัฒนา

กฎการตั้งชื่อในภาษาซี

ในภาษาซีจะมีกฎการตั้งชื่อให้กับ ตัวแปร, function และเลเบล ดังนี้


Copyright © 2024 thiti.dev |  v1.16.0 |  Privacy policy |  status | 

            วงแหวนเว็บ