การสืบทอด (Inheritance) ในภาษา Dart

wave
มานพ กองอุ่น 16 มิ.ย. 2019 22:37:33 6,459

เรียนรู้การสืบทอดจาก Base Class ไปยัง Sub Class โดยใช้คุณสมบัติการสืบทอดหรือ Inheritance

void main() {
  final person = Person(name: 'Manop Kongoon', age: 37, height: 1.75);
  print(person.getMe());
  final employee = Employee(taxCode: 'TAX123', salary: 50000);
  
}

class Person {
  Person({this.name, this.age, this.height});
  final String name;
  final int age;
  final double height;
  
  String getMe() => 
    "Hello, I'm $name. I'm $age years old, I'm $height meters tall";
}


class Employee extends Person {
  Employee({this.taxCode, this.salary});
  final String taxCode;
  final int salary;
}

ปัญหาคือเราไม่สามารถเรียกใช้ หรือกำหนด constructor ให้กับ Base Class ได้ ดังนั้นจะต้องใช้ Super Constructor

Super Constructor

void main() {
  final employee = Employee(name: 'Manop Kongoon', age: 37, height: 1.75, taxCode: 'TAX123', salary: 50000);
  employee.getName();
}

class Person {
  Person({this.name, this.age, this.height});
  final String name;
  final int age;
  final double height;
  
  String getMe() => 
    "Hello, I'm $name. I'm $age years old, I'm $height meters tall";
  
  void getName() => print("Hello, I'm $name");
}


class Employee extends Person {
  Employee({String name, int age, double height, this.taxCode, this.salary})
    :super(name: name, age: age, height: height);
  final String taxCode;
  final int salary;
}

Override toString() method

ในทุกๆ Base Class น้ันจะสืบทอดมาจาก Class Object เสมอ ซึ่งจะมี method toString() ซึ่งเราสามารถเขียน override ได้ (รายละเอียดเพิ่มเติมเรื่อง Object Class)

void main() {
  final employee = Employee(name: 'Manop Kongoon', age: 37, height: 1.75, taxCode: 'TAX123', salary: 50000);
  employee.getName();
  print(employee.toString());
}

class Person {
  Person({this.name, this.age, this.height});
  final String name;
  final int age;
  final double height;
  
  String toString() => 'name: $name, age: $age, height: $height';
  String getMe() => 
    "Hello, I'm $name. I'm $age years old, I'm $height meters tall";
  
  void getName() => print("Hello, I'm $name");
}


class Employee extends Person {
  Employee({String name, int age, double height, this.taxCode, this.salary})
    :super(name: name, age: age, height: height);
  final String taxCode;
  final int salary;
}

ผลลัพท์


Hello, I'm Manop Kongoon
name: Manop Kongoon, age: 37, height: 1.75

ควรใข้ @override เพื่อเป็นการระบุว่าเป็น method ที่ได้ override และ เราสามารถกำหนดใน Sub Class ได้เช่นกันดังนี้

void main() {
  final employee = Employee(name: 'Manop Kongoon', age: 37, height: 1.75, taxCode: 'TAX123', salary: 50000);
  employee.getName();
  print(employee.toString());
}

class Person {
  Person({this.name, this.age, this.height});
  final String name;
  final int age;
  final double height;
  
  @override
  String toString() => 'name: $name, age: $age, height: $height';
  String getMe() => 
    "Hello, I'm $name. I'm $age years old, I'm $height meters tall";
  
  void getName() => print("Hello, I'm $name");
}


class Employee extends Person {
  Employee({String name, int age, double height, this.taxCode, this.salary})
    :super(name: name, age: age, height: height);
  final String taxCode;
  final int salary;
  
  @override
  String toString() => '${super.toString()}, taxCode: $taxCode, salary: $salary';
}

ผลลัพท์

Hello, I'm Manop Kongoon
name: Manop Kongoon, age: 37, height: 1.75, taxCode: TAX123, salary: 50000

เราสามารถใช้ 

print(employee);

 


ความคิดเห็น

หากบทเรียนรู้มีความผิดพลาดประการใด หรือมีข้อเสนอแนะกรุณาแจ้ง contact@programmerthailand.com

เขียนบทเรียนรู้ของคุณ

รายละเอียด
  • ดู 6,459
  • รักเลย 0
  • หมวดหมู่ Flutter
  • เขียนเมื่อ
  • แก้ไขเมื่อ
  • Tags dart flutter inheritance
ข้อมูลผู้เขียน
มานพ กองอุ่น

มานพ กองอุ่น

เป็นสมาชิกเมื่อ: 18 ธ.ค. 2009