ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Dart] Class and namedConstructor
    Flutter/Dart 2023. 6. 22. 00:23

    constructor 는 initializer 와 같다

    그리고 constructor 를 여러개 만들수도 있다.

    방법은 아래와 같다.

    void main() {
          Man man = Man();
          Man man2 = Man.namedConstructor();
          Man man3 = Man.anotherContructor();
          print(man.name);
          print(man2.name);
          print(man3.name);
          // man1
          // man2 
          // man3
    }
    
    class Man {
      
      String? name;
      int? age;
    
      Man() {
        this.name = "man1";
        this.age = 1;
      }
      
      Man.namedConstructor() {
        this.name = "man2";
        this.age = 2;
      }
      
      Man.anotherContructor() {
        this.name = "man3";
        this.age = 3;
      }
    }
Designed by Tistory.