본문 바로가기

Dart

[Dart] 상속과 변수 초기화

부모/자식 클래스의 변수초기하는 방법

 

void main() {
  ECar tesla = ECar(7,6,5,4);
  GCar sonata = GCar(1,10,3);
  
  tesla.seat = 10;
  sonata.seat = 4;
  
  print('tesla: ' + tesla.currentSpeed(300).toString());
  print('sonata: ' + sonata.currentSpeed(250).toString());
  print('tesla : seat(${tesla.seat}), door(${tesla.door}), power(${tesla.power}), capacity(${tesla.capacity}), ');
  print('sonata : seat(${sonata.seat}), door(${sonata.door}), cc(${sonata.cc})');
}

class Car {
  int seat;
  int door;
  String color = 'white';
  int engineSize = 1000;
  
  Car(this.seat, this.door);
  
  int currentSpeed(int accel) {
    return accel * engineSize;
  }
  
}

class ECar extends Car {
  int power;
  int capacity;
  ECar(int seat, int door, this.power, this.capacity) : super(seat, door);

}

class GCar extends Car {
  int cc;
  
  GCar(int seat, int door, this.cc): super(seat, door);
}

 

출력결과는 아래와 같습니다.

 

tesla: 300000
sonata: 250000
tesla : seat(10), door(6), power(5), capacity(4), 
sonata : seat(4), door(10), cc(3)

 

 

 

 

'Dart' 카테고리의 다른 글

[Dart] Class - getter, setter  (0) 2021.05.27
[Dart] Class - 선언 및 생성자  (0) 2021.05.27
[Dart] Null Safety  (0) 2021.05.12
[Dart] Stream 처리.  (0) 2021.05.01
[Dart] Future, async-await  (0) 2021.04.30