Dart Memo : constructor

Dr_Skele·2023년 1월 3일
0

Dart

목록 보기
4/5

Dart has a special constructor that can be accessed the way we access methods. Such constructors can be much helpful to see what the code does. Rather than having constructors with same name and different parameters, dart provides a constructor with different parameters that can be named.

class Polygon{
  final String shape;
  int radius;

  //default constructor
  Polygon({required this.shape, required this.radius});

  //named constructor
  Polygon.triangle({required int rad}) : shape = 'triangle', radius = rad;
  Polygon.square({required int rad}) : shape = 'square', radius = rad;
}
profile
Tireless And Restless Debugging In Source : TARDIS

0개의 댓글