Dart Classes(클래스) - Cascade Operator, Enum, Abstract Class, Inheritance, Mixin
Cascade Notation(Cascade Operator)
기존에 클래스를 생성하고 해당 객체의 값을 변경하려면 다음과 같이 작성해야했다.
Dart에서는 이렇게 taek.~~~ 이 중복을 줄일 수 있도록 할 수 있다고 한다.
변경된 소스코드는 다음과 같다.
class Player {
String name;
int xp;
String team;
Player({
required this.name,
required this.xp,
required this.team,
});
void sayHello() {
print("Hi, my name is $name");
}
}
void main() {
var taek = Player(name: 'taek', xp: 1200, team: 'red')
..name = 'jung'
..xp = 12300
..team = 'blue'
..sayHello();
}
..으로 표시할 수 있으며, taek variable 뒤에 ; 을 없애고 마지막에 붙여준다.
클래스 내의 메서드도 호출할 수 있다.
Enums
Java에서 쓰는 Enum이랑 개념은 똑같다.
Dart의 Enum은 어떻게 작성하는지만 보고 넘어가자.
enum Team { red, blue }
enum XPLevel { begginer, medium, pro }
class Player {
String name;
XPLevel xp;
Team team;
Player({
required this.name,
required this.xp,
required this.team,
});
void sayHello() {
print("Hi, my name is $name");
}
}
void main() {
var taek = Player(
name: 'taek',
xp: XPLevel.medium,
team: Team.red,
)
..name = 'jung'
..xp = XPLevel.pro
..team = Team.blue
..sayHello();
}
Abstract Classes(추상 클래스)
Dart의 Abstract Class 또한 Java와 개념이 똑같다.
마찬가지로 작성법만 보고 넘어가겠다.
abstract class Human {
void walk();
}
enum Team { red, blue }
enum XPLevel { begginer, medium, pro }
class Player extends Human {
String name;
XPLevel xp;
Team team;
Player({
required this.name,
required this.xp,
required this.team,
});
void walk() {
print('i\'m walking');
}
void sayHello() {
print("Hi, my name is $name");
}
}
class Coach extends Human {
void walk() {
print('The coach is walking');
}
}
void main() {
var taek = Player(
name: 'taek',
xp: XPLevel.medium,
team: Team.red,
)
..name = 'jung'
..xp = XPLevel.pro
..team = Team.blue
..sayHello();
}
Inheritance(상속)
Flutter에서는 가끔씩만 사용되겠지만, 꼭 알아둬야하는 중요한 개념이다.
그리고 Java의 상속 개념을 잘 알고 있다면, 이것 또한 어떻게 작성되는지만 보자.
class Human {
final String name;
Human(this.name);
void sayHello() {
print("Hi my name is $name");
}
}
enum Team { blue, red }
class Player extends Human {
final Team team;
Player({
required this.team,
required String name,
}) : super(name);
@override
void sayHello() {
super.sayHello();
print('and I play for $team');
}
}
void main() {
var player = Player(
team: Team.red,
name: 'taek',
);
player.sayHello();
}
Mixins
Flutter에서 자주 사용된다.
앞의 소스코드에서 Human을 보면 생성자가 있었다.
그래서 Player 클래스를 만들 때 super를 써야 했었다.
Mixins는 생성자가 없는 클래스를 의미한다.
클래스에 프로퍼티들을 추가하거나 할 때 사용된다.
예를 들어 Player라는 클래스가 있다고 할 때, Mixin 클래스를 만들어보자.
mixin이라고만 써도 되고 mixin class라고 써도 된다.
with를 선언해서 mixin 클래스를 가져올 수 있다.
mixin Strong {
final double stengthLevel = 1500.99;
}
mixin class QuickRunner {
void runQuick() {
print('ruuuuuun!');
}
}
mixin class Tall {
final double height = 1.99;
}
enum Team { blue, red }
class Player with Strong, QuickRunner, Tall {
final Team team;
Player({
required this.team,
});
}
class Horse with Strong, QuickRunner {}
class Kids with QuickRunner {}
void main() {
var player = Player(
team: Team.red,
);
}
이 mixin 클래스는 단 하나의 클래스에 한번만 가져와서 사용할거면 의미가 없다.
mixin의 핵심은 여러 클래스에 재사용이 가능하다는 점이다.
Player, Horse, Kids 이 모든 클래스들은 with로 연결된 클래스의 프로퍼티들을 사용할 수 있다.
extend랑은 다르다.extend를 하게 되면 확장한 그 클래스는 부모 클래스가 되는거고, 자식 클래스는 부모 클래스를 super를 통해서 접근할 수 있고, 그 순간 부모 클래스의 인스턴스가 된다.Mixin은 with를 통해 선언되고, 역할은 단순히 Mixin 내부의 프로퍼티와 메서드들을 가져오는 것 뿐이다. 부모 클래스가 되거나 그러진 않는다. 한마디로 뺏어오는 것이라 보면 된다.
중요한 것은 Mixin은 생성자가 없어야된다.-> 이전에는 class ~~~ {} 로 선언하면 됐는데, 최신 버전은 mixin이라고 명시해주어야 한다.
Flutter를 배우기 위한 Dart언어 준비는 여기까지만 하도록 하겠다.Flutter 공부하러 가보자.
Dart The end