DartPad에서 설치 없이 연습 가능
노마드코더 Dart 시작하기에서 무료 수강 가능
class Player {
final String name = 'nico';
int xp = 1500;
void sayHello() {
print("Hi my name is $name");
}
}
void main() {
var player = Player();
player.sayHello();
}
class Player {
late final String name;
late int xp;
Player(String name, int xp) {
this.name = name;
this.xp = xp;
}
void sayHello() {
print("Hi my name is $name");
}
}
void main() {
var player = Player("nico", 1500);
player.sayHello();
var player2 = Player("lynn", 2500);
player2.sayHello();
}
class Player {
late final String name;
late int xp;
Player(this.name, this.xp);
void sayHello() {
print("Hi my name is $name");
}
}
class Player {
late final String name;
late int xp;
String team;
int age;
Player({
required this.name,
required this.xp,
required this.team,
required this.age,
});
}
void main() {
var player = Player(
name: "nico",
xp: 1500,
team: 'blue',
age: 21,
);
var player2 = Player(
name: "lynn",
xp: 2500,
team: 'blue',
age: 12,
);
}
class Player {
late final String name;
late int xp;
String team;
int age;
Player({
required this.name,
required this.xp,
required this.team,
required this.age,
});
Player.createBluePlayer({
required String name,
required int age,
}) : this.age = age,
this.name = name,
this.team = 'blue',
this.xp = 0;
Player.createRedPlayer(String name, int age)
: this.age = age,
this.name = name,
this.team = 'red',
this.xp = 0;
}
void main() {
var player = Player.createBluePlayer(
name: "nico",
age: 21,
);
var redPlayer = Player.createRedPlayer("lynn", 12);
}
class Player {
late final String name;
late int xp;
String team;
Player.fromJson(Map<String, dynamic> playerJson)
: name = playerJson['name'],
xp = playerJson['xp'],
team = playerJson['team'];
void sayHello() {
print("Hi my name is $name");
}
}
void main() {
var apiData = [
{
"name": "nico",
"team": "red",
"xp": 0,
},
{
"name": "lynn",
"team": "red",
"xp": 0,
},
{
"name": "dal",
"team": "red",
"xp": 0,
},
];
apiData.forEach((playerJson) {
var player = Player.fromJson(playerJson);
player.sayHello();
});
}
..
으로 대체할 수 있음..
은 바로 앞의 인스턴스(객체)를 가르킴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 nico = Player(name: 'nico', xp: 1200, team: 'red');
var potato = nico
..name = 'las'
..xp = 120000
..team = 'blue'
..sayHello();
}
enum Team { red, blue }
enum XPLevel { beginner, 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 nico = Player(name: 'nico', xp: XPLevel.medium, team: Team.red);
var potato = nico
..name = 'las'
..xp = XPLevel.pro
..team = Team.blue
..sayHello();
}
abstract class Human {
void walk();
}
enum Team { red, blue }
enum XPLevel { beginner, 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('im walking');
}
void sayHello() {
print("Hi my name is $name");
}
}
void main() {
var nico = Player(name: 'nico', xp: XPLevel.medium, team: Team.red);
nico.walk();
}
super
라는 키워드를 통해 부모 클래스와 상호작용할 수 있게 해줌super
생성자를 호출class Human {
final String name;
Human({required 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: name);
void sayHello() {
super.sayHello();
print('and I play for ${team}');
}
}
void main() {
var player = Player(team: Team.red, name: 'nico');
player.sayHello();
}
mixin class Strong {
final double strenghtLevel = 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});
}
void main() {
var player = Player(team: Team.red);
player.runQuick();
}