day_by_day

8. Typedef 본문

QA Automation/dart

8. Typedef

kokorii_ 2023. 5. 16. 00:05

1. 선언과 사용

main의 첫번째 줄에서 operation에는 add라는 함수가 들어있음 

void main(){
  
  Operation operation = add;
  
  int result = operation(10, 20, 30);
  print(result);
  
  operation = sub;
 
  int res2 = operation(30,20,10);
  print(res2);
  
  //아래와 같이 많이씀 
  int res3 = calculate(30,40,50, add);
  print(res3);
  
}

//signature : return type과 parameter의 형태 
typedef Operation = int Function(int x, int y, int z);

int add(int x, int y, int z) => x + y + z;
int sub(int x, int y, int z) => x - y - z;

int calculate(int x, int y, int z, Operation operation){
  return operation(x,y,z);
}

'QA Automation > dart' 카테고리의 다른 글

10. Immutable 프로그래밍과 Getter, Setter, private keyword  (2) 2023.05.16
9. class : 기본, 생성자와 this  (2) 2023.05.16
7. 함수(function)  (2) 2023.05.16
6. Map  (2) 2023.05.15
5. List  (4) 2023.05.15