속성와 메서드
- 메서드에 같은 이름의 변수가없으면 this를 사용하지않아도 해당 클래스의 속성에 접근 가능
class Player {
String name = 'dongwoo';
int age = 30;
void sayHello() {
print("hello my name is $name");
}
}
void main() {
var player = Player();
player.sayHello();
player.name = 'new name';
player.sayHello();
}
constructor
- late를 통해 constructor에서 할당 가능- final을 통해 수정 불가능
class Player {
late final String name;
late int age;
Player(String name, int age) {
this.name = name;
this.age = age;
}
void sayHello() {
print("hello my name is $name");
}
}
void main() {
var player = Player("dongwoo", 30);
player.sayHello();
player.name = 'new name';
player.sayHello();
}
constructor - 2 : positional arguments
class Player {
final String name;
int age;
Player(this.name, this.age);
void sayHello() {
print("hello my name is $name");
}
}
void main() {
var player = Player("dongwoo", 30);
player.sayHello();
}
constructor - 3 : named arguments
class Player {
final String name;
int age;
Player({
required this.name,
required this.age
});
void sayHello() {
print("hello my name is $name");
}
}
void main() {
var player = Player(
name: "dongwoo",
age: 30,
);
player.sayHello();
}
constructor - 4 : named constructor
class Player {
final String name;
int age;
String team;
Player.createBluePlayer({
required String name,
required int age,
}) : this.name = name,
this.age = age,
this.team = "blue";
Player.createRedPlayer({
required String name,
required int age,
}) : this.name = name,
this.age = age,
this.team = "red";
void sayHello() {
print("hello my name is $name");
}
}
void main() {
var player = Player.createBluePlayer(
name: "dongwoo",
age: 30,
);
var player2 = Player.createRedPlayer(
name: "hani",
age: 20,
);
player.sayHello();
player2.sayHello();
}
api data binding
class Player {
final String name;
int age;
Player.fromJson(Map<String, dynamic> playerJson)
: name = playerJson['name'],
age = playerJson['age'];
void sayHello() {
print("hello my name is $name");
}
}
void main() {
var apiData = [
{
"name": "dongwoo",
"age": 30,
},{
"name": "minji",
"age": 20,
},{
"name": "hani",
"age": 20,
},
];
apiData.forEach((playerJson) {
var player = Player.fromJson(playerJson);
player.sayHello();
});
}
cascade notation
class Player {
String name;
int age;
String team;
Player({
required String name,
required int age,
required String team,
}) : this.name = name,
this.age = age,
this.team = team;
void sayHello() {
print("hello my name is $name");
}
}
void main() {
var dongwoo = Player(
name:"dongwoo",
age: 30,
team: "redvelvet")
..name = 'minji'
..age = 20
..team = "newjeans";
}
enum
enum Team {redvelvet, newjeans}
class Player {
String name;
int age;
Team team;
Player({
required String name,
required int age,
required Team team,
}) : this.name = name,
this.age = age,
this.team = team;
void sayHello() {
print("hello my name is $name");
}
}
void main() {
var dongwoo = Player(
name:"dongwoo",
age: 30,
team: Team.redvelvet)
..name = 'minji'
..age = 20
..team = Team.newjeans;
dongwoo.sayHello();
}
abstract class
enum Team {redvelvet, newjeans}
abstract class Human {
void walk();
}
class Player extends Human {
String name;
int age;
Team team;
Player({
required String name,
required int age,
required Team team,
}) : this.name = name,
this.age = age,
this.team = team;
void sayHello() {
print("hello my name is $name");
}
void walk() {
print("player $name is walking...");
}
}
class Coach extends Human {
void walk() {
print("coach is walking...");
}
}
void main() {
var dongwoo = Player(
name:"dongwoo",
age: 30,
team: Team.redvelvet);
var hani = Coach();
dongwoo.walk();
hani.walk();
}
inheritance
enum Team {redvelvet, newjeans}
class Human {
final String name;
Human(this.nenum Team {redvelvet, newjeans}
class Human {
final String name;
Human({required this.name});
void sayHello() {
print("hello my name is $name");
}
}
class Player extends Human {
Team team;
Player({
required this.team,
required String name,
}) : super(name: name);
@override
void sayHello() {
super.sayHello();
print("hello my team is $team");
}
}
void main() {
var dongwoo = Player(
team: Team.redvelvet,
name: "dongwoo",
);
dongwoo.sayHello();
});
void sayHello() {
print("hello my name is $name");
}
}
class Player extends Human {
Team team;
Player({
required this.team,
required String name,
}) : super(name);
}
void main() {
var dongwoo = Player(
team: Team.redvelvet,
name: "dongwoo",
);
dongwoo.sayHello();
}
mixins
class Strong {
final double strenghtLevel = 1500.99;
}
class QuickRunner {
void runQuick() {
print("ruuuuuuuuun!");
}
}
class Tall {
final double height = 1.99;
}
enum Team {redvelvet, newjeans}
class Player with Strong, QuickRunner, Tall {
Team team;
Player({
required this.team,
});
void sayHello() {
print("hello my team is $team");
}
}
class Horse with Strong, QuickRunner {}
class Kid with QuickRunner {}
void main() {
var dongwoo = Player(
team: Team.redvelvet,
);
dongwoo.sayHello();
dongwoo.runQuick();
print(dongwoo.height);
}
참고
노마드코더 - Dart 시작하기 (무료강의)