Dart(2)

손병진·2021년 6월 14일
0

조건문

if() {} else if() {} else {}

swich(e) {
  case 1:
    break;
  case 2:
    break;
  default:
    break;
}
// if문 switch문 : javascript 와 동일하다

void main() {
  int total = 0;
  
  List<int> numberList = [2,3,5,2];
  
  for (int e = 0; e < numberList.length; e++){
    if(total == 10) {
      break;
      // 조건식을 멈춘다.
    }
    total += numberList[e];
  }
  
  for(int number in numberList){
    if(total == 10) {
      continue;
      // 다음 조건으로 넘어간다
      // break, continue 문법은 while 문에도 사용가능
    }
    total += number;
  }
  
  // 조건을 먼저 확인하고 코드 실행
  while(total < 10) {
    total++;
  }
  
  // 코드를 먼저 실행하고 조건 확인
  do {
    total++;
  }while(total < 10);
  
  
  print(total);
  
}

함수

파라미터

void main() {
  calcul1(1);
  calcul2(1, b: 1);
}

double calcul1 (double a, [double b = 2]){
  // 기본적인 위치기반 positional parameter(a)
  // optional parameter: 지정하지 않아도 디폴트값을 정해놓음
  return a*b;
}

double calcul2 (double a, {double b = 2}){
  // named parameter: 파라미터 이름을 지정하여 받음
  return a*b;
}

클래스

상속

void main() {
  final Castie = new Korean('castie', 'male', 1);
  
  Castie.sayCountry(); // 저는 한국인 입니다
  Castie.sayName(); // 저는 castie 입니다
}

// 부모 클래스 선언
class Human {
  final String name;
  final String gender;
  int id;

  Human(String name, String gender, int id)
      : this.name = name,
        this.gender = gender,
        this.id = id;

  void sayName() {
    print('저는 ${this.name} 입니다');
  }
}

// 자식 클래스
class Korean extends Human {
  // 상속받는 클래스이기 때문에 super 키워드를 사용하여 부모클래스에 대한 파라미터도 할당한다
  Korean(String name, String gender, int id) : super(name, gender, id);

  void sayCountry() {
    print('저는 한국인입니다');
  }
}

메서드 오버라이드

void main() {
  final parent = new Parent(3,4);
  parent.calcul(); // 7
  
  final child = new Child(3,4);
  child.calcul(); // 12
}

class Parent {
  int num1;
  int num2;
  
  Parent(this.num1, this.num2);
  
  void calcul () {
    print(num1 + num2);
  }
}

class Child extends Parent {
  int num1;
  int num2;
  
  Child(this.num1, this.num2) : super(num1, num2);
  
  // 부모클래스의 calcul 함수를 덮어쓴다
  
  void calcul () {
    print(num1 * num2);
  }
}

static & cascade

void main() {
  final castie = Privacy('castie', 29);
  
  // static 변수는 클래스에 직접 할당
  Privacy.country = 'korea';  
  castie.introduce(); // 저는 korea 에서 사는 castie 입니다
  
  // static 함수는 클래스에서 직접 실행
  Privacy.myCountry(); // 저는 korea 출신입니다
  
  
  // cascade
  Privacy.country = 'america'; // static 변수 재할당 가능
  new Privacy('harring', 30)
    ..introduce() //
    ..myAge();
    // 저는 america 에서 사는 harring 입니다
    // harring 의 나이는 30 입니다
  
}

class Privacy {
  static String country = '';
  String name;
  int age;
  
  Privacy(this.name, this.age);
  
  void introduce () {
    print('저는 $country 에서 사는 $name 입니다');
  }
  
  void myAge () {
    print('$name 의 나이는 $age 입니다');
  }
  
  static void myCountry () {
    print('저는 $country 출신입니다');
  }
}

List 주요 메서드

void main() {
  List test1 = [
    '첫번째',
    '두번째',
    '세번째',
    '네번째',
  ];
  
  print(test1.isEmpty); // false
  print(test1.isNotEmpty); // true
  print(test1.length); // 4
  print(test1.first); // 첫번째
  print(test1.last); // 네번째
  print(test1.reversed); // (네번째, 세번째, 두번째, 첫번째)

  test1.add('다섯번째');
  print(test1); // [첫번째, 두번째, 세번째, 네번째, 다섯번째]
  
  test1.addAll(['여섯번째', '일곱번째']);
  print(test1); // [첫번째, 두번째, 세번째, 네번째, 다섯번째, 여섯번째, 일곱번째]
}
  test1.remove('첫번째');
  print(test1); // [두번째, 세번째, 네번째, 다섯번째, 여섯번째, 일곱번째]
  
  test1.removeAt(0);
  print(test1); // [세번째, 네번째, 다섯번째, 여섯번째, 일곱번째]

Map 주요메서드

void main() {
  Map test2 = {
    'first' : 1,
    'second' : 2,
    'third' : 3,
  };
  
  print(test2.isEmpty); // false
  print(test2.isNotEmpty); // true
  print(test2.length); // 3
  print(test2.keys); // (first, second, third)
  print(test2.values); // (1, 2, 3)
  
  test2.addAll({'fourth' : 4});
  print(test2); // {first: 1, second: 2, third: 3, fourth: 4}

  test2['fifth'] = 5 ;
  print(test2); // {first: 1, second: 2, third: 3, fourth: 4, fifth: 5}
  
  test2.update('first', (prev)=>prev*10);
  print(test2['first']); // 10
  
  test2.update('sixth', (prev)=>prev*10, ifAbsent: ()=>10);
  print(test2); // {first: 10, second: 2, third: 3, fourth: 4, fifth: 5, sixth: 10}
  
  test2.remove('first');
  print(test2); // {second: 2, third: 3, fourth: 4, fifth: 5, sixth: 10}
  
  test2.removeWhere((key, value){
    return value == 10;
  });
  print(test2); // {second: 2, third: 3, fourth: 4, fifth: 5}
}
profile
https://castie.tistory.com

0개의 댓글