dart 문법2

정은하·2024년 1월 21일

Flutter

목록 보기
2/8
post-thumbnail

함수와 메서드

void main() {
  print(add(2,4));
}
//함수
int add(int a, int b){
  return a+b;
}
//메서드->클래스 내부에서 선언된 함수
class UserInfo{
  String name;
  int age;
  String hobby;
  
  void setStart(){
    
  }
}

positional parameter vs named parameter

//positional parameter
void serStart(String name, int age){
  print('$name, $age');
}
//named parameter->권장 사항
void serStart2({String name='홍길동', int age=24}){
  print('$name, $age');
}
//named parameter with required
void serStart3({required String name}){
  print('$name');
}
void main() {
 serStart('은하',25);
 serStart2(name:'은',age:26);
 serStart3(name:'은기');
}

클래스와 상속

  1. 클래스
class Person{
  //상태-멤버 변수
  String name;
  int age;
  
  //생성자(Constructor): 생성될 때 호출되는 것
  Person(this.name,this.age);
  
  //행동-메서드(함수)
  void sayHello(){
    print('안녕하세요 저는 $name이고 $age살 입니다.');
  }
}
void main() {
  //클래스 인스턴스 생성
  Person person=Person('은하',25); 
  Person person2=Person('은기',27);
  
  //함수 내 메서드 호출
  person.sayHello();
}
  1. 상속
    -기존 클래스이 특성을 다른 클래스에서 재사용하고 확장하는 메커니즘
    -부모 클래스(super class)와 자식 클래스(sub class)간의 상속 관계
class Person{
  //상태-멤버 변수
  String name;
  int age;
  
  //생성자(Constructor): 생성될 때 호출되는 것
  Person(this.name,this.age);
  
  //행동-메서드(함수)
  void sayHello(){
    print('안녕하세요 저는 $name이고 $age살 입니다.');
  }
}
//상속
class Man extends Person{
  Man(String name, int age) : super(name, age);
  
  @override
  void sayHello(){
    //부모 클래스에 정의되었던 함수 호출
    super.sayHello();
    print('\n제 성별은 남자입니다.');
  }
}

void main() {
  //클래스 인스턴스 생성
  Person person=Person('은하',25); 
  Person person2=Person('은기',27);
  
  var man=Man('남기',30);
  //함수 내 메서드 호출
  person.sayHello();
  person2.sayHello();
  
  //상속
  man.sayHello();
}

생성자와 선택적 매개변수

생성자: 클래스의 인스턴스를 초기화하는 특별한 매서드, 클래스를 생성할 때 가장 먼저 호출되는 것
기본 생성자: 생략 가능, 클래스에서 별도의 생성자를 정의하지 않는 다면 기본 생성자가 사용됨

class Person{
  //기본 생성자
  Person();
}

class Person2{
  String name;
  int age;
  //매개 변수가 존재하는 생성자
  Person2(this.name, this.age);
}

class Person3{
  String name;
  int age;
  Person3({this.name='홍길동', this.age=27});
}

class Person4{
  String name;
  int age;
  Person4({required this.name, required this.age});
}

void main() {
 var person=Person();
 var person2=Person2('은하',25);
 var person3=Person3(name:'홍실동');
 var person4=Person4(name:'홍실동',age:10);
}

enum

열거형-타입 정의에 보통 사용, 상수들의 그룹을 정의할 때 유용

enum Color {
  red,
  green,
  blue,
  yellow,
}

void main() {
  //enum 값을 변수에 할당
  Color myColor=Color.blue;
  
  if(myColor==Color.red){
    print('red');
  } 
  else if(myColor==Color.green){
    print('green');
  }
  else if(myColor==Color.blue){
     print('blue');
  }
  else if(myColor==Color.yellow){
     print('yellow');
  }
  print(Color.red.index);
 for(int i=0;i<Color.values.length;i++){
    print(Color.values[i]);
  }
}

future와 await을 활용한 비동기 프로그래밍

-future: 비동기 작업의 결과, 완료 상태를 나타내는 객체
-동기: 작업이 순차적으로 실행

void main() {
 playComputerGame();
}

void playComputerGame(){
  startBoot(); //1. 컴퓨터를 부팅한다.
  startInternet(); //2. 인터넷을 실행한다.
  startGame(); //3. 게임을 실행한다.
}
void startBoot(){
  print('1. boot completed');
}
void startInternet(){
  print('2. Internet completed');
}
void startGame(){
  print('3. Game completed');
}

-비동기: 작업이 순차적으로 실행되지 않으며 동시에 여러 작업을 처리 가능

void main() {
 playComputerGame();
}

void playComputerGame(){
  startBoot(); //1. 컴퓨터를 부팅한다.
  startInternet(); //2. 인터넷을 실행한다.
  startGame(); //3. 게임을 실행한다.
}
void startBoot(){
  print('1. boot completed');
}
void startInternet(){
  //sleep vs delay
  Future.delayed(Duration(seconds:3), (){
    print('2. Internet completed');
  });
  print('delay completed');
}
void startGame(){
  print('3. Game completed');
}

-await: 비동기 함수 내에서 사용되며 await 뒤에 나오는 결과 값이 완료될 때까지 실행을 일시적으로 중단

void main() {
 playComputerGame();
}

void playComputerGame(){
  startBoot(); //1. 컴퓨터를 부팅한다.
  startInternet(); //2. 인터넷을 실행한다.
  startGame(); //3. 게임을 실행한다.
}
void startBoot(){
  print('1. boot completed');
}

Future<void> startInternet() async {
  //sleep vs delay
  await Future.delayed(Duration(seconds:3), (){
    print('2. Internet completed');
  });
  print('delay completed');
}

void startGame(){
  print('3. Game completed');
}
//결과 값
1. boot completed
3. Game completed
2. Internet completed
delay completed

흐름까지 제어가 필요할 때

void main() {
 playComputerGame();
}

Future<void> playComputerGame() async {
  startBoot(); //1. 컴퓨터를 부팅한다.
  await startInternet(); //2. 인터넷을 실행한다.
  startGame(); //3. 게임을 실행한다.
}
void startBoot(){
  print('1. boot completed');
}

Future<void> startInternet() async {
  //sleep vs delay
  await Future.delayed(Duration(seconds:3), (){
    print('2. Internet completed');
  });
  print('delay completed');
}

void startGame(){
  print('3. Game completed');
}
//결과값
1. boot completed
2. Internet completed
delay completed
3. Game completed
profile
If you remain stagnant, you won't achieve anything

0개의 댓글