[LMS] Dart 기본 문법 [ Class & Object ]

nouveau·2023년 11월 29일
0

Class & Object

Class 란?

"객체를 정의해놓은 것" 또는 "객체의 설계도 또는 틀"

class Person{
  var name;
  var age;

  Person(this.name, this.age);

  void printInfo() {
    print("name : $name , age : $age");
  }
}

Object(객체)란?

클래스에 정의된 내용대로 메모리에 생성된 것을 뜻한다.

var person = Person('Nouveau', 1);
* 시스템에 정의 돼 있지 않는 이름으로 선언해야 한다.
=> 객체의 구조 설계 후 재사용하여 반복작업을 줄이고, 유지보수의 효율성을 위해 사용한다 생각한다.

Inheritance(상속)

상속은 객체지향 프로그래밍의 꽃이라 할 수 있으며, 부모(상위) 클래스의 멤버를 자식(하위) 클래스로 내려주는 것(=상속)을 의미 한다.

class Person {
  String name;
  int age;

  Person(this.name, this.age);
}

class Korean extends Person {
  String nation;

  Korean(super.name, super.age, this.nation);
}

main() {
  Korean nouveau = Korean("nouveau", 20, "Korea");
  print(nouveau.nation);
}

Polymorphism(다형성)

위키 : 다형성이란 프로그램 언어 각 요소들(상수, 변수, 식, 객체, 메소드 등)이 다양한 자료형(type)에 속하는 것이 허가되는 성질을 가리킨다.

상속받은 객체의 속성이 객체마다 다양하게 재구성 되는 것

abstract class Person {
  String name;
  int age;

  Person(this.name, this.age);

  void hello();
}

class Korean extends Person {
  String nation = "Korea";

  Korean(super.name, super.age);

  
  void hello () {
    print("안녕하세요.");
  }
}

class American extends Person {
  String nation = "America";

  American(super.name, super.age);

  
  void hello () {
    print("Hi.");
  }
}

main() {
  Korean nouveau = Korean("nouveau", 20);
  print(nouveau.nation);
  nouveau.hello();

  American james = American("james", 20);
  print(james.nation);
  james.hello();
}

0개의 댓글