สมัครเรียนโทร. 085-350-7540 , 084-88-00-255 , ntprintf@gmail.com

Tutorial DART

L01 DART INTRO L02 DART HOWTO L03 DART GETTING START L04 DART SYNTAX L05 DART VARIABLE 01 L06 DART VARIABLE 02 L07 DART FUNCTION L08 DART OPTIONAL NAMED POSITONAL L09 DART LIST L10 DART CLASS L11 DART INHERITANCE MIXIN L12 DART EXCEPTION L13 DART GENERATOR AND ITERATOR L14 DART OPERATION

การสืบทอด Inheritance,Mixin ในภาษา Dart

เรียนเขียนโปรแกรมง่ายๆกับ Expert Programming Tutor ในบท การสืบทอด Inheritance,Mixin 


เรามาทำความรู้จัก Inheritance หรือการสืบทอด ในภาษา Dart

 เป็นวิธีการที่ทำให้ object หนึ่งสามารถใช้งาน property และ method จาก parent   class  ที่ทำการสืบทอดได้ หรือเข้าใจอย่างง่ายคือ class หนึ่งๆ สามารถสืบทอด property และ method จาก อีก class หนึ่งได้ สมมติเช่น class A สืบทอด class B เราจะเรียก class A และ class B ได้เป็นดังนี้
class B ก็คือ SUPER class หรือ BASE class หรือ PARENT class class A ก็คือ SUB class หรือ CHILD class  โดยในการใช้งานการสืบทอด จะใช้ extends keyword ในการกำหนด อย่างกรณีตามตัวอย่างด้านบน ก็จะได้เป็น

class B{ }
class C{ }
// class A สืบทอดจาก class B
class A extends B{ }

เราสามารถทำการสืบทอด class ได้เพียง class เดียวเท่านั้น กล่าวคือ  ไม่สามารถสืบทอด class A จาก class B และ class C พร้อมกันได้ // class A extends B,C{} // error เพื่อให้เข้าใจการสืบทอดง่ายขึ้น ขอยกตัวอย่างเกียวกับ Animal class, Dog class และ Cat class ประกอบคำอธิบาย

class Animal{
  String color; // สี
    void eat(){} // กินอาหารได้
}
class Dog{
  String color; // สี
  String breed; // สายพันธ์
  void bark(){} // เห่าได้
}
class Cat{
  String color;  // สี
  int age; // อายุ
    void meaw(){}  // ร้องเหมียวได้
}

class ทั้ง 3 ล้วนมี property และ method ของตัวเองซึ่งเป็นได้ทั้ง instance variable หรือ instance method โดยทั้ง Dog และ Cat มี property ที่ชื่อว่า color ซึ่งสามารถใช้ property นี้จาก Animal class ได้ ดังนั้น เราสามารถทำการสืบทอด Animal class ไปยัง Dog / Cat class ได้ดังนี้

class Animal{
  String color; // สี 
  void eat(){} // กินอาหารได้
}
class Dog extends Animal{
  String breed; // สายพันธ์
  void bark(){} // เห่าได้
}
class Cat extends Animal{
  int age; // อายุ

  void meaw(){}  // ร้องเหมียวได้
}

เมื่อทำการสืบทอด class จะทำให้ Child class ได้รับ หรือสามารถเรียกใช้ property และ method ของ Parent class ได้ นั่นคือ Dog และ Cat จะมี color และ eat()  ที่เป็น variable และ method ของ Animal class ที่ได้รับสืบทอดมานั่นเอง ดูตัวอย่างการเรียกใช้งาน

void main () {
  var dog = Dog();
  dog.breed = 'Pug'; 
  dog.color = 'black'; // color property จาก Animal class
  dog.eat(); // eat() method จาก Animal class
  dog.bark();
  var cat = Cat();
  cat.age = 4;
  cat.color = 'white'; // color property จาก Animal class
  cat.eat(); // eat() // method จาก Animal class
  cat.meaw();
}

ก่อนไปรายละเอียดต่อไป ขอทบทวนอีกครั้งว่า ในภาษา Dart ทุกอย่างล้วนเป็น object และทุกๆ class ในภาษา Dart ก็ล้วนสืบทอดมาจาก Object class ซึ่งเป็น Super class อีกที ดังนั้น ทุกๆ class จะได้รับการสืบทอด property ต่อไปนี้มาได้ ได้แก่

- toString() // method ที่คืนค่าข้อความที่อธิบายถึง object นั้นๆ

- hashCode // ตัวเลขค่า Hash Code หรือรหัสเฉพาะตัวของ object นั้นๆ ไว้ใช้ในการเปรียบเทียบค่า

-  opperator หรือตัวดำเนินการ ( == ) สำหรับเปรียบเทียบ สอง object ใดๆ 

  เรามาทดสอบลองเรียกใช้งาน property และ method ทั้ง 3 ร่วมกับ Dog และ Cat object  คำอธิบายแสดงในโค้ด จะได้เป็นดังนี้

void main () {
  var dog = Dog();
  print(dog.hashCode); // hashCode property จาก Object super class
  // output: 790618802 ค่าตัวเลขแสดงสภาวะของ object ไว้ใช้ในการเปรียบเทียบ ==
  // ค่า hashCode จะเปลี่ยนแปลงทุกครั้ง 
  print(dog.toString()); // toString() method จาก Object super class
  // output: Instance of 'Dog'

  var cat = Cat();
  print(cat.hashCode); // hashCode property จาก Object super class
  // output: 130547424 ค่าตัวเลขแสดงสภาวะของ object ไว้ใช้ในการเปรียบเทียบ ==
  // ค่า hashCode จะเปลี่ยนแปลงทุกครั้ง
  print(cat.toString()); // toString() method จาก Object super class
  // output: Instance of 'Cat'

  if(dog == cat){
    print("Same");
  }else{
    print("not same");
  }
  // output: not same
  var dog2 = dog;
  if(dog == dog2){
    print(dog.hashCode); // ค่าจะเท่ากับ hashCode ของ dog2
    print(dog2.hashCode); // ค่าจะเท่ากับ hashCode ของ dog
    print("Same");
  }else{
    print("not same");
  } 
  // output:
 // 790618802
// 790618802
// Same   
}

 ต่อมามารู้จักกับ Mixin สิ่งนึงในหลายๆภาษา เช่น Java ไม่สนับสนุน การสืบทอดแบบหลาย (Multiple Inheriance) โดยตรง ซึ่งจะสามารถสืบทอดได้เพียง class เดียวเท่านั้น ทำให้หากต้องการทำ Multiple Inheritance จริงๆจำเป็นต้องไปใช้วิธีอื่นมาช่วย เช่น interface , composit แทน และในภาษา Dart ก็ใช้แนวทางนี้เช่นกัน คือ สามารถสืบทอดได้เพียง     class เดียว แต่ Dart มองว่าการใช้ interface แบบใน Java เพื่อพยายามสร้างสิ่งที่คล้ายกับ Multiple Inheritance นั้น ไม่ทำให้ code อ่านง่ายอย่างที่ควรจะเป็น ใน Dart จึงใช้วิธีการที่เรียกว่า mixin เพื่อให้การทำใช้งานง่ายและดูสะอาดขึ้น การใช้ mixin นั้นง่ายมาก คือ สร้าง class หรือ abstract class ที่ต้องการให้เป็น Parent ก่อน

abstract class PersonA
{  String name;
 speak() {
    print("I'm A");
  }
}
  class PersonB{
 int age;
  speak() {
   print("I'm B");
  }
}
  class PersonC{
  bool single;
  speak() {
    print("I'm C");
  }
}
 class Human {
  speak() {
    print("I'm Human");
  }
}

จากนั้นก็ใช้ keyword ว่า with ตามด้วย class ที่ต้องการให้ไปสืบทอด หากมีหลายอื่นคั่นด้วย , (comma) เช่น class Man สืบทอดจาก Human , PersonA , PersonB , PersonC และสามารถใช้ตัวแปรและ method จากทั้ง PersonA , PersonB , PersonC ได้

class Man extends Human with PersonA, PersonB, PersonC {
  say() {
    print(name);
    print(age);
    print(single);
  }
}

ซึ่ง class ที่สืบทอดด้วย mixin นี้ จะต้องไม่มีการสืบทอดจาก class อื่น

Overriding in Mixinในภาษา Dart


ในกรณีที่ มีตัวแปร หรือ method ซ้ำกันใน class ระหว่าง class ที่สืบทอดโดยตรงกับ การสืบทอดแบบ mixin กันเอง ทำให้เกิด override ผลคือจะใช้ mixin อันท้ายสุดเสมอ ก็คือ Mixin เป็น Top Level เสมอ เช่น มี mixin 3 class ซึ่งมี method ตัวนึง คือ speak() จากนั้น class Man สืบทอด mixin จากทั้ง 3 class ทำให้ speak() ทับซ้อนกัน

mixin PersonA{
   speak() {
      print("I'm A");
   }
} 
mixin PersonB{
   speak() {
      print("I'm B");
   }
}
 mixin PersonC{
   speak() {
      print("I'm C");
   }
}
 class Human {
  speak() {
    print("I'm Human");
  }
}
 class Man extends Human with PersonA, PersonB, PersonC {
 say() {
    speak();
  }
}

เมื่อเราเรียก speak() การทำงานจะใช้ speak() ของ class ไหน?

Man man = Man();
man.say();

      คำตอบคือ class PersonC เพราะ PersonC คือ class Mixin ตัวสุดท้ายหรือหลังสุด ที่ใส่ให้ Man นั่นเอง

   แม้ว่าเราจะเรียก super.speak(); มันก็ยังจะเรียก จาก PersonC เพราะ speak() ได้ override ทับทุก class แม้แต่ Human ที่สืบทอดแบบ extens โดยตรงก็ตาม

class Man extends Human with PersonA, PersonB, PersonC {
  say() {
    super.speak();
    speak();
  }
}

“on” Keyword ในภาษา Dart


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

...
mixin PersonC on PersonB {
  speak() {
    super.speak();
    print(age);
    print("I'm C");
  }
}
class Man extends Human with PersonA, PersonB, PersonC {
  say() {
    speak();
  }
}

สรุป


  สำหรับภาษา Dart ก็มีฟีเจอร์ในการทำ inheritance เทียบเท่ากับภาษาสมัยใหม่ทั่วๆ ไปแต่อาจจะมีรูปแบบการเขียนต่างกันเล็กน้อย เช่นการไม่มี interface แต่ก็แทนที่ด้วยการที่เราสามารถ implements จากคลาสตรงๆ ได้เลย
     Mixin เป็นความสามารถนึงของภาษา Dart ที่มาช่วยการทำ Multiple Inheritance ได้ง่ายขึ้นนั่นเอง ซึ่งไม่จำเป็นต้องใช้ keyword mixin ก็ได้ สามารถใช้ abstract class , class แต่เพื่อความเฉพาะเจาะจงว่าเป็น mixin ที่ไม่สามารถนำไปใช้แบบ class หรือ abstract class ได้ ก็สามารถใช้ keyword ว่า mixin แทนนั่นเอง

สรุปบทความ https://dev.to/centrilliontech/dart-105-inheritance-mixin-57a4
https://benzneststudios.com/blog/dart/how-to-use-mixin-in-dart-language/



บทความนี้อาจจะมีที่ผิด กรุณาตรวจสอบก่อนใช้

หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor

ไม่อยากอ่าน Tutorial อยากมาเรียนเลยทำอย่างไร?

สมัครเรียน ONLINE ได้ทันทีที่ https://elearn.expert-programming-tutor.com

หรือติดต่อ

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM

แผนที่ ที่ตั้งของอาคารของเรา

C Article


C++ Article


Java Article


C#.NET Article


VB.NET Article


Python Article


Golang Article


JavaScript Article


Perl Article


Lua Article


Rust Article


Article


Python


Python Numpy


Python Machine Learning



แผนผังการเรียนเขียนโปรแกรม

Link อื่นๆ

Allow sites to save and read cookie data.
Cookies are small pieces of data created by sites you visit. They make your online experience easier by saving browsing information. We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies.

Copyright (c) 2013 expert-programming-tutor.com. All rights reserved. | 085-350-7540 | 084-88-00-255 | ntprintf@gmail.com

ติดต่อเราได้ที่

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM
แผนที่ ที่ตั้งของอาคารของเรา