1. Abstract Class!
abstract class Human {
void walk();
}
enum House { gyu, hyun }
class Player extends Human {
House name;
int power = 130;
final String neverChange;
Player({required this.name, required this.power, required this.neverChange});
Player.createGyuFamily({required House name, required int power})
: this.name = name,
this.power = power,
this.neverChange = 'man';
Player.clone(Player player)
: this.name = player.name,
this.power = player.power,
this.neverChange = player.neverChange;
void sayHello() {
print("Hi my name is $name and i'm $neverChange");
}
void walk() {
print("Hi i'm walking!");
}
}
void main() {
var player = Player.createGyuFamily(name: House.gyu, power: 11);
print(player.name.name);
}
abstract class [이름] { method 적기 } 의 형식으로 abstract class를 생성할 수 있다. 이 abstract class의 주 목적은 특정 class에 abstract class에서 정의한 method를 강제할 수 있다. 즉, 해당 abstract class를 extends 하는 class는 해당 abstract class에 있는 method들을 모두 갖고 있다고 확신할 수 있다.
2. Inheritance!
class Human {
final String name;
int age;
Human({required this.name, required this.age});
void sayHello() {
print('Hi! my name is $name and $age year\'s old');
}
}
enum Team { blue, red }
class Player extends Human {
final Team team;
Player({required this.team, required String name, required int age})
: super(age: age, name: name);
Player.withParent({required Human human, required this.team})
: super(age: human.age, name: human.name);
@override
void sayHello() {
super.sayHello();
print('And my team is ' + team.name);
print('\n');
}
}
void main() {
var player = Player(team: Team.blue, name: "gyu", age: 12);
player.sayHello();
var human = Human(name: "hyun", age: 11);
var player1 = Player.withParent(human: human, team: Team.red);
player1.sayHello();
human.sayHello();
}
특정 class를 상속하는 class는 위와 같이 사용할 수 있다. main함수의 첫번째 부분에서 사용한 방식은 부모 class가 사용할 값까지 모두 인자로 넘겨주는 방식이고 두번째 방법은 부모 객체 자체를 넘겨주는 방식이다.
그리고 이 값을 부모 객체에게 넘겨주기 위한 방법으로 constructor에서 super()를 사용한다.
부모의 method를 overriding 하기 위해서는 @override 어노테이션과 함께 super를 사용해 특정 함수를 overriding 한다(근데 @override 안붙여도 잘 됨)
3. Mixin!
class Human {
final String name;
int age;
Human({required this.name, required this.age});
void sayHello() {
print('Hi! my name is $name and $age year\'s old');
}
}
enum Team { blue, red }
class BeTaller {
final int height = 190;
}
class BeStronger {
final int power = 200000;
}
class RunFaster {
void running() {
print("run faster!!");
}
}
class Player extends Human with BeStronger, BeTaller, RunFaster {
final Team team;
Player({required this.team, required String name, required int age})
: super(age: age, name: name);
Player.withParent({required Human human, required this.team})
: super(age: human.age, name: human.name);
void sayHello() {
super.sayHello();
print('And my team is ' + team.name);
print('\n');
}
}
void main() {
var player = Player(team: Team.blue, name: "gyu", age: 12);
player.sayHello();
var human = Human(name: "hyun", age: 11);
var player1 = Player.withParent(human: human, team: Team.red);
player1.sayHello();
human.sayHello();
player1.running();
print(player1.height);
print(player1.power);
}
Mixin class는 생성자(constructor)가 없는 class이다. 이는 다른 class에서 with을 통해 extends 처럼 사용할 수 있다. extends와의 다른점은 with으로 가져온 class들과는 상속관계가 없이 단순히 해당 Mixin class 내부의 property와 method를 가져와서 사용할 수 있다는 것이다.
위의 상황처럼 BeTaller, BeStronger, RunFaster class를 생성자 없이 생성하고 이를 with로 가져오면 main함수에서 처럼 해당 객체가 Mixin class들의 각 property와 method를 사용할 수 있다.
반복되는 특성이 있다면 Mixin class로 정의하고 with으로 가져다 쓰면 코드의 반복을 줄일 수 있다.
Dart 빠르게 보기는 여기서 끝!!
개인적으로 느낀 Dart의 느낌은.. 구글에서 만든 Go언어랑은 느낌이 확연히 다르다. 오히려 Java와 좀 더 유사한..? 느낌이 있는데 Java보다 심플한 느낌이고 또 게임을 만드는데 유용하겠다 라는 생각이 들었다. (개인적 견해임! ㅎㅎ)
이제 Flutter를 배워보겠다.
'IT 일기 > Flutter!' 카테고리의 다른 글
Dart를 빠르게 알아보자 - class! #1 (0) | 2023.04.18 |
---|---|
Dart를 빠르게 알아보자 - function! (2) | 2023.04.17 |
Dart를 빠르게 알아보자 - Data Type! (0) | 2023.04.16 |
Dart를 빠르게 알아보자 - 변수 (0) | 2023.04.16 |