Flutter

Flutter 기본2(Class)

끄적a 2022. 1. 27. 17:15

Class

생성자

아래와 같은 방법들이 있다. 개인적인 생각으로 Fruit2방법을 가장 많이 사용하지 않을까 싶다. 

//기본
class Fruit {
  String? name;
  String? color;

  Fruit(String name, String color){
    this.name = name;
    this.color = color;
  }
}

class Fruit2 {
  String? name;
  String? color;
  
   Fruit2(this.name, this.color);
//    Fruit(this.name, this.color){
//            print('Constructor: $name($color)!');
//       }   
}

//초기화 리스트
class Fruit3 {
  String? name;
  String? color;

  Fruit3(String name, String color)
    : name = name, 
      color= color;
//   Fruit(String name, String color)
//     : name = name, 
//       color= color{
//            print('Constructor: $name($color)!');
//       }     
}

//   이름있는 생성자
class Fruit4 {
  String? name;
  String? color;

  Fruit4.init(String name, String color){
      this.name = name;
      this.color = color;
  }
}

//  리다이렉팅 생성자
class Fruit5 {
  String? name;
  String? color;

  Fruit5(this.name, this.color);
 	Fruit5.init(String name) : this(name, 'Green');
  
}

void main() {
  Fruit fruit = Fruit('Apple', 'Red');
  Fruit2 fruit2 = Fruit2('Banana', 'Yellow');
  Fruit3 fruit3 = Fruit3('BlueBerry', 'Blue');
  Fruit4 fruit4 = Fruit4.init('Remon', 'Yellow');
//   Fruit4 fruit4 = Fruit4('Apple', 'Red');//error
  Fruit5 fruit5 = Fruit5.init('melon');
//   Fruit5 fruit5 = Fruit5('Apple','Red');//not error

  printFruitName(fruit);  //Fruit is: Apple/Red!
  printFruitName(fruit2); //Fruit is: Banana/Yellow!
  printFruitName(fruit3); //Fruit is: BlueBerry/Blue!
  printFruitName(fruit4); //Fruit is: Remon/Yellow!
  printFruitName(fruit5); //Fruit is: melon/Green!
}

printFruitName(var object){
      print('Fruit is: ${object.name}/${object.color}!');
}

Private 

변수명 앞에 _를 추가함으로써, Private 변수를 생성, 다른 언어와 다르게 같은 파일 안에 있으면 Private에 접근 가능하다. 

class Fruit {
  String? _name;

  Fruit(this._name);

  void printName() {
    print('Fruit is ${this._name}!');
  }
}

void main() {
  Fruit fruit = new Fruit('Apple');
  fruit.printName();
  print(fruit._name);// It's possibe because of same file.
}

Getter/Setter

Private변수에는 get/set을 설정 할 수 있음. 보통 '_'제외한 이름으로 만듬.

class Fruit {
  String? _name;
  Fruit(this._name);
  
  String get name {
    print("call get : $_name");
    return _name ?? '';
  }
  set name(String name) {
    print("call set : $name");
    _name = name;
  }
}

void main() {
  Fruit fruit = Fruit('Apple');
  fruit.name; // call get : Apple
  fruit.name = 'Banana'; // call set : Banana
}

인터페이스

Interface라는 키워드 대신 class를 사용하여 인터페이스를 정의하고, implements로 사용합니다.

class Food {
  String? name;

  void printName() {}
}

class Fruit implements Food {
  @override
  String? name;

  Fruit(this.name);

  @override
  void printName() {
    print('Fruit is $name!');
  }
}

void main() {
  Fruit fruit = Fruit('Apple');
  fruit.printName();
}

Cascade Operator

클래스를 선언함과 동시에, 클래스에 함수들을 동시에 사용

class Fruit {
  String? name;
  String? color;

  Fruit(this.name, this.color);
  
  void printName() {
    print('My name is $name!');
  }

  void printColor() {
    print('I am $color');
  }
}

void main() {
  Fruit fruit = Fruit('Apple', 'Red');
  fruit.printName(); //My name is Apple!
  fruit.printColor(); //I am Red

  Fruit('Apple', 'Red')
    ..printName()   //My name is Apple!
    ..printColor(); //I am Red
}

 

참조:

https://dev-yakuza.posstree.com/ko/flutter/dart/class/

https://zakkum.tistory.com/86