본문 바로가기
버킷리스트/앱 만들기

오공완(with 코드팩토리) #20, #19 (Method overriding, Static Keyword)

by 또또도전 2024. 6. 26.
반응형

# 이곳에 쓴 내용은 앱 만들기라는 버킷리스트를 달성하기 위해 플러터를 공부하면서 정리하고 있는 내용입니다.

플러터에 대해 아는 것이 거의 없기 때문에 정리하면서 오류가 있을 수 있습니다.

오류를 발견하신 분은 댓글 남겨 주시면 감사하겠습니다.

 

   Method

 

Method는 클래스 안의 함수

@는 decorator라고 부른다고.. 검색을 통해 알아보니 decorator는 기존 함수를 다른 함수로 바꾸거나 수정할 수 있다고 한다.

void main() {
  
  Parent parent = new Parent(3);
  Child child = new Child(3);
  
  print(parent.calculate());
  print(child.calculate());
  
}

class Parent {
  
  final int number;
  
  Parent(
  int number,
  ) : this.number = number;
  
  int calculate(){
    return this.number * this.number;
  }
}

class Child extends Parent {
  Child(
  int number,
  ) : super(
  number,
  );
  @override
  int calculate(){
    
    int result = super.calculate();
    return result + result;
  }
}


정상 작동되니 당황스럽다. int number했을 때 값이 없어서 null 어쩌고 메시지 또 뜨겠네 했는데 아닌가 보다. 알다가도 모르겠다.

 

  Static Keyword

 

static keyword 인스턴스에 귀속되지 않고 클래스에 통채로 귀속될 때 사용된다고 함. (당연히 이해 못 함.)

 

String 값에 아무 값이나 넣어줬다.

 

void main() {
  Employee seulgi = new Employee('슬기');
  Employee chorong = new Employee('초롱');
  
  seulgi.printNameAndBuilding();
  chorong.printNameAndBuilding();
  
  Employee.building = '여의도 위워크';
  seulgi.printNameAndBuilding();
  chorong.printNameAndBuilding();
  
  Employee.building = '을지로 위워크';
  seulgi.printNameAndBuilding();
  chorong.printNameAndBuilding();
  
}

class Employee {
  static String building='위워크';
  String name;
  
  Employee(
  String name,
  ) : this.name = name;
  
  void printNameAndBuilding(){
    print('제 이름은 ${this.name}입니다. $building 건물에서 근무하고 있습니다.');
  }
}

 

오늘도 수고했어~^^;;

반응형