ฝึกภาษา Dart ภาษาใหม่ของ Google #2

Patiphan Suwanich
2 min readApr 30, 2020

--

เหมือนเดิมนะครับ บทความนี้ไม่ได้มาสอนนะครับ ผมจะเขียนแนวๆ Dart เขียนอย่างไรได้บ้าง ขอมาเล่าสู่กันฟังดีกว่า

นี่เป็นบทความต่อจาก

Contents

  • Inheritance
  • Mixins
  • Interfaces
  • Abstract classes

กดลิ้งก์ Try-dart นี้เป็น IDE Online ครับ

Inheritance

Inheritance คือการสืบทอดคุณสมบัติจากคลาสแม่สู่คลาสลูก

main() {
var p = SmartTelevision();

p.turnOn();
p.upgradeApps();
p.turnOff();
}
class Television {
turnOn() => print("Open");

turnOff() => print("Off");
}
class SmartTelevision extends Television {
void turnOn() {
super.turnOn();
print("Connecting network");
}

@override
turnOff(){
print("Turn off network and television");
}

upgradeApps() => print("Upgrade App");
}

จากโค้ดตัวอย่างผมลองทำ Dart ใช้ extends คราสแม่เพื่อให้คลาสลูกใช้super ในการอ้างถึงฟังก์ชั่นในคลาสแม่ได้ ทำให้ลดความซ้ำซ้อนของโค้ดได้และถ้าคลาสลูกอยากใช้ฟังก์ชั่นชื่อเดียวกับคลาสแม่ก็ให้ใส่ override ก็จะเป็นการเขียนทับฟังก์ชั่นเดิม

Mixins

Mixins เป็นอีกวิธีการนำหลายๆคลาสกลับมาใช้ใหม่หรือเรียกว่า Multiple Inheritance

ผู้อ่านสามารถประกาศ mixin หรือ class ก็ได้นะครับ พอตอนจะใช้ก็ใช้with เป็น keyword หลังจากนั้นใส่ชื่อ mixin หรือ class

main() {
var r = Rabbit();
var t = Turtle();
r(); // I'm Rabbit
t(); // I'm Turtle

t.walk(); // walking

r.walk(); // walking
r.run(); // running
r.stop(); // ***

t.stop(); // Stop walk
}
class Walker {
walk() => print("walking");

stop() => print("Stop walk");
}
mixin Runner {
run() => print("running");

stop() => print("Stop run");
}
class Animal {
callMe(type) => print("I'm $type");
}
class Rabbit extends Animal with Runner , Walker {
call() => super.callMe("Rabbit");

stop() => super.stop();
}
class Turtle extends Animal with Walker {
call() => super.callMe("Turtle");

stop() => super.stop();
}

Mixin vs Class

เมื่อเราประกาศเป็น mixin แล้วไม่สามารถใช้ extends keyword ได้ mixin ต้องคู่กับ with keyword เท่านั้น ส่วน class สามารถใช้ keyword ได้ทั้งคู่ครับ

What will happen if the function name is the same?

ผู้อ่านน่าจะสังเกตุว่า r.stop(); แล้วเกิดสงสัยว่าจะ r จะใช้ฟังก์ชั่น stop ตัวไหน?
Dart จะเลือกฟังก์ชั่นหลังเสมอ

เช่นถ้าผมใช้ with Runner , Walker 
r.stop(); จะปริ้น Stop walk
แต่ถ้าผมใช้ with Walker, Runner
r.stop(); จะปริ้น Stop run

แต่การจะทำแบบนี้ได้มีข้อจำกัดนั่นคือเมธอดทั้ง 2 ตัวจะต้องมี return-type และ parameters ที่เหมือนกันทุกอย่างครับ

“on” Keyword

Mixin สามารถสืบทอดกันเองได้โดยใช้ on ตามด้วย ชื่อ Mixin หรือ class

Interfaces

ภาษา Dart จะไม่มี interface keyword นะแต่ Dart จะมองว่าทุกๆ class เป็น interface สามารถนำไปใช้งานได้โดยใช้ implements keyword
เมื่อนำ class ที่มี constructor อยู่ constructor จะไม่ถูกนำไปใช้นะครับ

// A person. The implicit interface contains greet().
class Person {
// In the interface, but visible only in this library.
final _name;
// Not in the interface, since this is a constructor.
Person(this._name);
// In the interface.
String greet(String who) => 'Hello, $who. I am $_name.';
}
// An implementation of the Person interface.
class Impostor implements Person {
get _name => '';
String greet(String who) => 'Hi $who. Do you know who I am?';
}
void main() {
var p = Person("Benz");
var i = Impostor();
print(p.greet("Dart")); // Hello, Dart. I am Benz.
print(i.greet("Dart")); // Hi Dart. Do you know who I am?
}

Implements multiple

ผู้อ่านสามารถ implements class ได้มากกว่า 1 นะครับ

class Point implements Comparable, Location {...}

Abstract Class

abstract class คือ class ที่เราสามารถกำหนด property และ method สำหรับวางโครงหรือเป็นหน้าตา interface ที่เราจะนำไปใช้งานต่อได้ง่ายเพราะสามารถทำ abstract method หรือฟังก์ชั่นว่างๆครับ

abstract class Person {
// Define constructors, fields, methods...
void greet(String who); // Abstract method.
}
// An implementation of the Person interface.
class Impostor implements Person {
String greet(String who) => 'Hi $who. Do you know who I am?';
}
void main() {
var i = Impostor();
print(i.greet("Dart"));
}

เท่านี้ผู้อ่านน่าจะเข้าใจวิธีการเขียนภาษา Dart แบบพื้นฐานกันแล้ว
บทความต่อไปผมน่าจะเขียนถึง Flutter ติดตามกันได้เลยครับ 👏

ถ้ามีอะไรติชมเขียนมาได้เลยนะครับ ผมจะได้เอาไปปรับปรุงในบทความต่อๆไป ขอบคุณที่อ่านจนจบครับ

--

--

Patiphan Suwanich
Patiphan Suwanich

Written by Patiphan Suwanich

Work at Major Development | Interest in Blockchain | looking for opportunities in my programmer’s life

No responses yet