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

IDN กับการจดโดเมนภาษาไทย

IDN หรือเรียกเต็มๆว่า Internationalized Domain Name คือ ชื่อโดเมนที่สามารถประกอบด้วยอักขระ (character) อื่น ๆ ที่นอกเหนือไปจากรหัส ASCII ได้ ซึ่งเดิมทีการตั้งชื่อโดเมนจะถูกจำกัดอยู่เพียงแค่การใช้ตัวอักษรภาษาอังกฤษ (อักษรโรมัน) A-Z, ตัวเลขอารบิก 0-9 และเครื่องหมายยัติภังค์ (-) เท่านั้น เนื่องจากข้อจำกัดของระบบชื่อโดเมน (domain name system - DNS) ที่ใช้อยู่แต่เดิม

วิธี update software ใน ubuntu

วิธี update software ใน ubuntu สามารถทําได้ด้วย 3 คําสั่งนี้

วิธีสร้าง Private Repository Github แบบฟรีๆ

สวัสดีครับ ปัจจุบันเว็บที่ให้บริการเกี่ยวกับ version control มีอยู่มากมายหลายเว็บ แต่ที่นิยมกันก็คือ Github Github เปิดให้ใช้บริการฟรีแต่มีข้อจํากัด ไม่สามารจะสร้าง Private Repository ได้ ถ้าใครอยากจะสร้างก็ต้องเสียเงิน ในบทความนี้ผมจะมาแนะนําวิธีใช้งาน  Private Repository กันแบบฟรีๆ วิธีการก็คือ เราจะต้องส่งคําร้องขอใช้งานสําหรับนักศึกษาโดยจะเป็น free plans for educational


Copyright © 2019 - 2024 thiti.dev |  v1.19.0 |  Privacy policy |  status | 

            วงแหวนเว็บ