Tuesday, January 21, 2025
HomeLập Trình DartTìm hiểu Constructor trong Dart

Tìm hiểu Constructor trong Dart

Khái niệm

Trước khi tìm hiểu về Constructor thì bạn nên xem về các khái niệm Class và Object trong Dart.
Constructor là một method đặt biệt dùng để khởi tạo object, được gọi tự động khi object tạo ra và có thể khởi tạo các giá trị ban đầu của các thành viên trong object như properties.

Constructor không có tham số

Ví dụ 1: khai báo constructor không có tham số, lúc này construcor sẽ được gọi khi đối tượng được khởi tạo

class Fruit {
  String? name;
  String? color;

// Constructor sẽ gọi khi khởi tạo object 
Fruit(){
  print('Constructor is called !');
}

  void ShowInfo() {
    print('Fruit name: $name, fruit color: $color');
  }
}

void main() {
  Fruit fruit = Fruit();
  fruit.name = "apple";
  fruit.color = "blue";
}

Ở ví dụ trên khi khởi tạo Fruit fruit = Fruit(); thì Constructor sẽ gọi khi khởi tạo object, do đó kết quả thu được sẽ là dòng chữ Constructor is called !

Constructor có truyền tham số

class Fruit {
  String? name;
  String? color;

// Constructor có hai parameter
  Fruit(String name, String color) {
    print('Constructor is called !');
    this.name = name;
    this.color = color;
    ShowInfo();
  }

  void ShowInfo() {
    print('Fruit name: $name, fruit color: $color');
  }
}

void main() {
  // Nếu bạn không có truyền hai đối số ở đây thì chương trình báo lỗi
  // Fruit fruit = Fruit();
  Fruit fruit = Fruit("apple", "red");
  /*
  Constructor is called !
  Fruit name: apple, fruit color: red
   */
}

Chương trình trên có hai tham số ở constructor là name và age sau đó dùng this.name (thuộc tính) để gán giá trị là biến name (tham số), và tương tự cho color.
Sau đó gọi hàm ShowInfor() để lấy ra các thông tin tương ứng.

Construtor theo kiểu Single Line

Với kiểu này bạn sẽ viết this.name và this.color trực tiếp trong constructor. Lúc này nó sẽ tự gán cho thuộc tính tương ứng ở class Fruit

class Fruit {
  String? name;
  String? color;

// Constructor có hai parameter
  Fruit(this.name, this.color) {
    print('Constructor is called !');

    // Không dùng vì được thay thể bởi kiểu viết Single Line Constructor
    // this.name = name;
    // this.color = color;
    ShowInfo();
  }

  void ShowInfo() {
    print('Fruit name: $name, fruit color: $color');
  }
}

void main() {
  Fruit fruit = Fruit("apple", "red");
  /*
  Constructor is called !
  Fruit name: apple, fruit color: red
   */
}

Constructor với optional parameters.

class Fruit {
  String? name;
  String? color;
  String? weight; 
  double? price;

  Fruit(String this.name, String this.color,[this.weight,this.price]) {
    print('Constructor is called !');
    ShowInfo();
  }

  void ShowInfo() {
    print('Fruit name: $name, fruit color: $color');
    print('Fruit weight: $weight, price: $price');
  }
}

void main() {
  Fruit fruit = Fruit("apple", "red");
}

Ở ví dụ trên weight và price là hai tham số tuỳ chọn, không bắt buộc phải có, mặc định nếu không gán giá trị thì nó sẽ null.
Kết quả :
Constructor is called !
Fruit name: apple, fruit color: red
Fruit weight: null, price: null

Constructor với Named Parameters

Khi dùng named parameter bạn cần đặt các tham số trong cặp dấu ngoặc nhọn và khi truyền đối số thì cần viết tường minh tên của tham số. Hãy xem ví dụ bên dưới

class Fruit {
  String? name;
  String? color;

  Fruit({this.name, this.color}) {
    print('Constructor is called !');
    ShowInfo();
  }

  void ShowInfo() {
    print('Fruit name: $name, fruit color: $color');
  }
}

void main() {
  Fruit fruit = Fruit(name: "apple", color: "red");
}

Constructor có giá trị mặc định

Cũng đặt các parameter trong cặp dấu ngoặc nhọn nhưng lúc này có thêm giá trị ở các tham số

class Fruit {
  String? name;
  String? color;

  Fruit({this.name = "lemon", this.color="Yellow"}) {
    print('Constructor is called !');
    ShowInfo();
  }

  void ShowInfo() {
    print('Fruit name: $name, fruit color: $color');
  }
}

void main() {
  Fruit fruit = Fruit();
}

Ở ví dụ bên trên, khi tạo object chúng ta không truyền đối số thì mặc định chúng sẽ lấy các giá trị mặc định

Kết quả
Constructor is called !
Fruit name: lemon, fruit color: Yellow

Nguyễn Minh Châu
Nguyễn Minh Châuhttps://nhatkydev.com
Hi guys ! I'm a software developer. I love programming and new technologies. I create non-professional content on this website, you can only view it for reference purposes.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular