본문 바로가기
카테고리 없음

Dart 객체지향 프로그래밍

by joa-yo 2022. 11. 9.
반응형
void main() {
  Computer samsungLaptop = Computer('samsung', '100', ['8GB', '8GB']);  
  samsungLaptop.printManufacturer();
  samsungLaptop.printPrice();
  samsungLaptop.printMemoryr();
  
  //제조사별로 클래스를 나눠보자
  print('-------- SamsungComputer --------');
  SamsungComputer sComputer = SamsungComputer('100', ['8GB']);
  sComputer.printManufacturer();
  sComputer.printPrice();
  sComputer.printMemoryr();
  
  print('-------- AppleComputer --------');
  AppleComputer aComputer = AppleComputer('150', ['4GB']);
  aComputer.printManufacturer();
  aComputer.printPrice();
  aComputer.printMemoryr();
}

class Computer {
  String _manufacturer;
  String _price;
  List<String> _memory;

  //named constructor1
  Computer(this._manufacturer, this._price, this._memory);
  
  void printManufacturer () {
     print('$_manufacturer에서 제조하였습니다.');
  }
  void printPrice() {
     print('$_price만원입니다.');
  }
  
  void printMemoryr() {
     print('$_memory의 메모리 용량입니다.');
  }
}

class SamsungComputer extends Computer {
  SamsungComputer(
    String price,
    List<String> memory
  ) : super('samsung', price, memory); 
}

class AppleComputer extends Computer {
  AppleComputer(
    String price,
    List<String> memory
  ) : super('apple', price, memory); 
}

생성자

기본생성자

void main() {
    Computer computer = new Computer();
    computer.on();
    computer.off();
}

class Computer {
    String manufacturer = '';
    String price = '';
    List<String> memory = ['8GB', '4GB'];
    
    void on() {
    	print('$manufacturer on');
    }
    
    void off() {
    	print('$manufacturer off');
    }
}

 

여러가지 생성자의 형태

void main() {
//     Computer computer = new Computer();
//     computer.on();
//     computer.off();

  Computer samsungLaptop = new Computer('samsung', '100');
  samsungLaptop.on();
  samsungLaptop.off();

  Computer macLaptop = Computer('mac', '150');
  macLaptop.on();
  macLaptop.off();
  
  Computer thinkpad = Computer.manufacturer('lenovo');
  thinkpad.on();
  thinkpad.off();
}

class Computer {
  String manufacturer = '';
  String price = '';
  List<String> memory = ['8GB', '4GB'];

  //constructor3
//   Computer(String manufacturer, String price)
//       : this.manufacturer = manufacturer,
//         this.price = price;
  
  
  //constructor2
  Computer(this.manufacturer, this.price);
  
  //named constructor1
  Computer.manufacturer(this.manufacturer);

  void on() {
    print('$manufacturer on');
  }

  void off() {
    print('$manufacturer off');
  }
}

생성자1은 간결하게 생성자2처럼 표현할 수 있다.  

 

immutable(변경할 수 없는, 불변의) 프로그래밍에 관련된 생성자

void main() {
  Computer samsungLaptop = Computer('samsung', '100', ['8GB', '8GB']);
  samsungLaptop.on();
  samsungLaptop.off();
}

class Computer {
  final String manufacturer;
  final String price;
  final List<String> memory;

  //named constructor1
  const Computer(this.manufacturer, this.price, this.memory);

  void on() {
    print('$manufacturer on');
  }

  void off() {
    print('$manufacturer off');
  }
}

i

생성된 객체 값을 수정하지 못하게 아려면, 모든 멤버변수를 final로 지정하고, 모든 멤버변수를 생성자의 파라미터에 넣어준다. 이렇게 해주면, 멤버변수 수정시 에러가 발생하게된다.

 

const와 const가 아닌 객체의 차이

void main() {
  Computer samsungLaptop = Computer('samsung', '100', ['8GB', '8GB']);
  Computer samsungLaptop2 = Computer('samsung', '100', ['8GB', '8GB']);
  
  bool isSamsungSame = samsungLaptop == samsungLaptop2;
  print('samsungLaptop $isSamsungSame');	//false
  
        
  Computer macLaptop = const Computer('mac', '150', ['8GB', '8GB']);  
  Computer macLaptop2 = const Computer('mac', '150', ['8GB', '8GB']);
        
  bool isMacSame = macLaptop == macLaptop;
  print('const macLaptop $isMacSame');		//true
}

class Computer {
  final String manufacturer;
  final String price;
  final List<String> memory;

  //named constructor1
  const Computer(this.manufacturer, this.price, this.memory);

  void on() {
    print('$manufacturer on');
  }

  void off() {
    print('$manufacturer off');
  }
}

 

getter와 setter

void main() {
  Computer samsungLaptop = Computer('samsung', '100', ['8GB', '8GB']);
  print(samsungLaptop.firstMemory);
  
  //setter
  samsungLaptop.firstMemory = '4GB';
  
  //getter
  print(samsungLaptop.firstMemory);
}

class Computer {
  String manufacturer;
  String price;
  List<String> memory;
  
  Computer(this.manufacturer, this.price, this.memory);

  //getter
  String get firstMemory{
    return memory[0];
  }
  
  //setter
  set firstMemory(String memory){
    this.memory[0] = memory;
  }
}

 

private 

void main() {
  Computer samsungLaptop = Computer('samsung', '100', ['8GB', '8GB']);
  print(samsungLaptop.first_memory);
  
  //setter
  samsungLaptop.first_memory = '4GB';
  
  //getter
  print(samsungLaptop.first_memory);
  print(samsungLaptop._manufacturer);
  print(samsungLaptop._price);
  print(samsungLaptop._memory);
  samsungLaptop.on();
  samsungLaptop._off();
}

class Computer {
  String _manufacturer;
  String _price;
  List<String> _memory;

  //named constructor1
  Computer(this._manufacturer, this._price, this._memory);

  void on() {
    print('$_manufacturer on');
  }

  void _off() {
    print('$_manufacturer off');
  }
  
  //getter
  String get first_memory{
    return _memory[0];
  }
  
  //setter
  set first_memory(String _memory){
    this._memory[0] = _memory;
  }
  
  get manufacturer {
    return _manufacturer;
  }
}

함수나 변수명 앞에 "_"를 붙이면, private가 된다. private는 같은파일에서는 호출 및 수정이 가능하지만, 다른 파일에서는불가능하다.

 

상속 & override

void main() {
  Computer samsungLaptop = Computer('samsung', '100', ['8GB', '8GB']);  
  samsungLaptop.printManufacturer();
  samsungLaptop.printPrice();
  samsungLaptop.printMemoryr();
  samsungLaptop.on();
  
  //제조사별로 클래스를 나눠보자
  print('-------- SamsungComputer --------');
  SamsungComputer sComputer = SamsungComputer('100', ['8GB']);
  sComputer.printManufacturer();
  sComputer.printPrice();
  sComputer.printMemoryr();
  sComputer.on();
  
  print('-------- AppleComputer --------');
  AppleComputer aComputer = AppleComputer('150', ['4GB']);
  aComputer.printManufacturer();
  aComputer.printPrice();
  aComputer.printMemoryr(); 
  aComputer.on();
  
  
}

class Computer {
  String _manufacturer;
  String _price;
  List<String> _memory;

  //named constructor1
  Computer(this._manufacturer, this._price, this._memory);
  
  void on() {
    print('$_manufacturer on');
  }
  
  void printManufacturer () {
     print('$_manufacturer에서 제조하였습니다.');
  }
  void printPrice() {
     print('$_price만원입니다.');
  }
  
  void printMemoryr() {
     print('$_memory의 메모리 용량입니다.');
  }
}

class SamsungComputer extends Computer {
  SamsungComputer(
    String price,
    List<String> memory
  ) : super('samsung', price, memory); 
  
  @override
  void on() {
    print('Hello My name is $_manufacturer');
  }
}

class AppleComputer extends Computer {
  AppleComputer(
    String price,
    List<String> memory
  ) : super('apple', price, memory); 
  
  @override
  void on() {
    print('Hello. this is $_manufacturer computer');
  }
}

출력결과

samsung에서 제조하였습니다.
100만원입니다.
[8GB, 8GB]의 메모리 용량입니다.
samsung on
-------- SamsungComputer --------
samsung에서 제조하였습니다.
100만원입니다.
[8GB]의 메모리 용량입니다.
Hello My name is samsung
-------- AppleComputer --------
apple에서 제조하였습니다.
150만원입니다.
[4GB]의 메모리 용량입니다.
Hello. this is apple computer

 

Static

void main() {
  
  Developer.company = '조아로그';
    
  Developer developer1 = Developer('김아무개');
  developer1.printName();
  
  Developer developer2 = Developer('이아무개');
  developer2.printName();
  
  Developer.company = '로아로아';
  Developer.printCompany();
}

class Developer {
  static String? company;
  String name;
  
  Developer(
    this.name
  );
  
  static void printCompany(){
    print('$company 소속 개발자입니다.') ;
  }
  
  void printName() {
    print('$company 소속 개발자 $name입니다.') ;
  }
}

출력

조아로그 소속 개발자 김아무개입니다.
조아로그 소속 개발자 이아무개입니다.
로아로아 소속 개발자입니다.

 

implements

void main() {
  Robot robot = Robot(30, 30);
  robot.printCurrentPosition();
}

abstract class MoveInterface {
  int positionX = 0;
  int positionY = 0 ;
  
  void printCurrentPosition() {}
}

class Robot implements MoveInterface {
  int positionX;
  int positionY;
  
  Robot(this.positionX, this.positionY);
  
  @override
  void printCurrentPosition() {
    print('Robot position : $positionX, $positionY');
  }
}

출력

Robot position : 30, 30

 

반응형

댓글