Created at : 2025-01-10 02:47
Auther: Soo.Y
๐๋ฉ๋ชจ
1. class ์ ์ธ
dart์์๋ class ์ ์ธ์ ์ฃผ๋ก ์ฌ์ฉํ๊ณ class์ ์ด๋ฆ์ ์ฃผ๋ก ๋๋ฌธ์๋ก ์์ํ๋ค. void main
๊ณผ ๊ฐ์ด ์ค๊ดํธ {}
๋ก ๊ตฌ๋ถํ๊ณ class์์ ์ ์ธ๋๋ ๋ณ์๋ ๋ฐ๋์ ํ์
์ ์ ์ธํด์ผ ํ๋ค. var
์ ์ธ์ด ์๋๋ค๋ ๋ป์.
class ๋ด๋ถ์์ this.name
๊ณผ ๊ฐ์ด this
๋ผ๋ ๋ฌธ๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ class์ ์์ฑ์์ ๋ช
์ํ ์ ์๋๋ฐ ๋ฐ๋์ ์ฌ์ฉํ ํ์๋ ์๋ค. ๋จ, class๋ด ํจ์์์ ๋์ผํ ์ด๋ฆ์ ๋ณ์๋ช
์ด ์๋ค๋ฉด ์๋ก ๊ตฌ๋ถํ๊ธฐ ์ํด์ this.name
์ class์ ์์ฑ ๋ณ์์ด๊ณ name
์ ํจ์ ๋ด์ ์ ์ธ๋ ๋ณ์์์ ์๋ฏธํ๋ค.
- property
- method
class Player {
// ๋ณ์๋ ํ์
์ ๋ฐ๋์ ๋ช
์ํด์ผ ํ๋ค. class์์ var ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
fianl String name = "Sooyoung";
int xp = 1500;
void sayHeelo() {
print("Hi my name is $name");
// print("Hi my name is $this.name");
}
}
2. Constructors
์์ฑ์(Constructors)๋ฅผ ์ฌ์ฉํด์ class์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ ๊ฐ์ฒด์ ์์ฑ์ ์ ๋ ฅ ๋ฐ์ ํ๋ผ๋ฏธํฐ๋ก ์ด๊ธฐํ๊ฐ ๊ฐ๋ฅํ๋ค. Dart์์๋ ๊ธฐ๋ณธ ์์ฑ์๋ฟ๋ง ์๋๋ผ ์ฌ์ฉ์ ์ ์ ์์ฑ์๋ ๋ง๋ค ์ ์์ผ๋ฉฐ, ์์ฑ์๋ ํด๋์ค ์ด๋ฆ๊ณผ ๋์ผํ๊ฒ ์ ์๋๋ค.
class Player{
// late๋ฅผ ์ฌ์ฉํด์ final์ด์ง๋ง, ๋์ค์ ์
๋ ฅ ๋ฐ์ ์ ์๊ฒ ์ค์ ํจ
late final String name;
late int xp;
Player(String name, int xp) {
this.name = name;
this.xp = xp;
}
}
void main() {
var player = Player('Sooyoung', 1500);
print("${player.name}, ${player.xp}");
}
์ด์ ์์ฑ์๋ฅผ ์ข ๋ ์ฌํํ๊ฒ ๋ง๋ค์ด๋ณด์.
class Player{
final String name;
int xp;
Player(this.name, this.xp); // ์ด๋ ๊ฒ ์์ฑ์๋ฅผ ํ์ค๋ก ๊ฐ๋ฅํจ
// ๋จ, ์ง๊ธ์ positional parameters์ด๋ค.
}
3. Named Constructor Parameters
Named constructor parameters๋ ์์ฑ์์์ ๋งค๊ฐ๋ณ์์ ์์๋ฅผ ๊ธฐ์ตํ ํ์ ์์ด ์ด๋ฆ์ ์ฌ์ฉํด ๊ฐ์ ์ ๋ฌํ ์ ์๊ฒ ํด์ค๋ค.
์ค๊ดํธ{}
๋ฅผ ์ฌ์ฉํ๋ฉด named constructor parameters๋ก ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค. ํจ์์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๊ณผ ๋์ผํจ.
class Player{
final String name;
int xp;
// ๊ธฐ๋ณธ๊ฐ์ ์ฃผ๋์ง required๋ฅผ ์ฃผ๋์ง...
Player({required this.name, required this.xp});
}
void main() {
var player = Player(
name: "Sooyoung",
xp: 1500,
);
print("${player.name}, ${player.xp}");
}
์ค์ต ๋ฌธ์ 1 : Book
ํด๋์ค๋ฅผ ์์ฑํ๊ณ , ์ด๋ฆ ์๋ ์์ฑ์๋ฅผ ์ฌ์ฉํ์ฌ title
๊ณผ author
๊ฐ์ ์ด๊ธฐํํ์ธ์.
class Book {
String title;
String author;
Book({required this.title, required this.author});
}
void main() {
var book = Book(title: '1984', author: 'George Orwell');
print('Title: ${book.title}, Author: ${book.author}');
}
4. Named Constructor
Named constructor๋ ํด๋์ค๋ฅผ ์ ์ํ ๋ ์ฌ๋ฌ ์์ฑ์๋ฅผ ์ ๊ณตํ ๋ ์ฌ์ฉํ๋ค. ์ด๋ Classname.ConstructorName
ํ์์ผ๋ก ์์ฑ์๋ฅผ ์ ์ํ๊ณ , ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํน์ ๋ชฉ์ ์ ๋ง๋ ์์ฑ์๋ฅผ ์ ํํ ์ ์๊ฒ ํ๋ค.
์์)
class Player{
final String name;
int xp;
String team;
Player({required this.name, required this.xp, required this.team});
Player.createBluePlayer({required String name, required int xp}) :
this.xp = xp,
this.name = name,
this.team = 'Blue';
}
void main() {
var player = Player.createBluePlayer(
name: "Sooyoung",
xp: 1500,
);
print("${player.name}, ${player.xp}, ${player.team}");
}
5. Cascade Notation
Cascade notation(..)์ ๊ฐ์ ๊ฐ์ฒด์ ๋ํด ์ฌ๋ฌ ๋ฉ์๋ ํธ์ถ์ด๋ ์์ฑ ์ค์ ์ ๊ฐ๋จํ ํํํ ์ ์๋๋ก ๋์์ค๋ค.
์๋ ์์์ ๊ฐ์ด user์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ๊ฐ ์์ฑ์ ๊ฐ์ ๋ณ๊ฒฝํ๋ ค๊ณ ํ๋ฉด user.name, user.email
์ ๊ฐ์ด ์ผ์ผ์ด ์ธ์คํด์ค์ ์ด๋ฆ๊ณผ ํจ๊ป ์์ฑ์ ์์ฑํด ์ค์ผ ํ๋ค. ํ์ง๋ง, cascade notation์ ์ฌ์ฉํ๋ฉด user
๋ผ๋ ์ค๋ณต๋๋ ๋จ์ด๋ฅผ ์๋ตํ ์ ์๋ค.
class User {
String name = '';
String email = '';
void printUser() {
print('Name: $name, Email: $email');
}
}
void main() {
var user = User();
user.name = 'Alice';
user.email = 'alice@example.com';
user.printUser();
}
๋จผ์ var user = User();
๋ค์ ์ธ๋ฏธ์ฝ๋ก ์ ์ญ์ ํ๊ณ user
๋ฅผ ์ ๋ถ๋ค .
๋ก ํํํ๋ฉฐ ๋ง์ง๋ง์ผ๋ก ๋ฐ๊พธ๋ ์์ฑ๋ง ์ ์ธํ๊ณ ๋งจ ๋ค์ ;
์ ์ ๋ถ ๋ค ์ญ์ ํ๋ค. ์ด๋ ๊ฒ ์์ฑ๋ ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
void main() {
var user = User()
..name = 'Alice'
..email = 'alice@example.com'
..printUser(); // ๋ฉ์๋ ํธ์ถ๋ ๊ฐ๋ฅํจ
}
6. Enums
Enum์ ์ ํด์ง ๊ฐ ์งํฉ์ ํํํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋ ์จ ์ํ๋ฅผ sunny
, rainy
, cloudy
์ ๊ฐ์ด ํํํ ์ ์๋ค.
Enum์ enum
ํค์๋๋ก ์ ์ํ๋ฉฐ, switch ๋ฌธ๊ณผ ํจ๊ป ์ฌ์ฉํ ์๋ ์๋ค.
์ค์ต ๋ฌธ์ 1 Weather
enum์ ์์ฑํ๊ณ , sunny
, rainy
, cloudy
๊ฐ์ ์ถ๊ฐํ์ธ์.
enum Weather { sunny, rainy, cloudly }
void main() {
var today = Weather.sunny;
switch (today) {
case Weather.sunny:
print('It is sunny!');
break;
case Weather.rainy:
print('It is rainy!');
break;
case Weather.cloudly:
print('It is cloudly!');
break;
}
}
7. Abstract Classes
์ถ์ํด๋์ค๋ ์ธ์คํด์ค๋ฅผ ์ง์ ์์ฑํ ์ ์๋ ํด๋์ค์ด๋ค. ์ถ์ํด๋์ค๋ ์์ํด๋์ค์์ ๊ตฌํํด์ผ ํ๋ ๋ฉ์๋๋ฅผ ํฌํจํ๊ณ ์๋ค. ์์์ ํตํด ๋ค์ํ ์์ํด๋์ค์ ๊ณตํต ๊ธฐ๋ฅ์ ์ ๊ณตํ๊ฑฐ๋, ํ์์ ์ผ๋ก ๊ตฌํํด์ผ ํ๋ ๋ฉ์๋๋ฅผ ๊ฐ๋ฐ์๊ฐ ๊ฐ์ ๋ก ๊ตฌํํ๋๋ก ํ ์ ์๋ค.
abstract class Shape {
double area();
}
class Circle extends Shape {
double radius;
Circle(this.radius);
@override
double area() => 3.14 * radius * radius;
}
8. Inheritance(์์)
์์์ ๊ธฐ์กด ํด๋์ค์ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์ฌ์ฌ์ฉํ๊ฑฐ๋ ํ์ฅํ๊ธฐ ์ํด์ ์ฌ์ฉ๋๋ค. Dart์์ ์น์์ extends
ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉฐ, ์์ํด๋์ค๋ ๋ถ๋ชจํด๋์ค์ ๋ชจ๋ ์์ฑ๊ณผ ๋ฉ์๋์ ์ ๊ทผํ ์ ์๋ค.
๋ํ super
ํค์๋๋ฅผ ํตํด ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค.
์ฌ๊ธฐ์๋ override
๋ฅผ ์ฌ์ฉํ์ฌ ๋ถ๋ชจํด๋์ค์์ ์ ์๋ honk
๋ฅผ ๋ณํํ๋ค. ๋จ, super.honk()
๋ฅผ ํธ์ถํ ์ ์๊ธฐ ๋๋ฌธ์ ์ค์ง์ ์ผ๋ก ๋ฎ์ด์ฐ๋ ํ์๊ฐ ์๋๊ณ ๋ฉ์๋์ ์ฐ์ ์์๊ฐ ์๋ค๊ณ ๋ณด๋ ๊ฒ์ด ์ข ๋ ์ ํํ๋ค๊ณ ์๊ฐ๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋ฉ๋ชจ๋ฆฌ ์ธก๋ฉด์์๋ ์๋ก ๋ค๋ฅธ ๊ณณ์ ์ ์ฅ๋๋ฏ๋ก override
์ ์ง์ญ์ธ ๋ฎ์ด์ฐ๊ธฐ๋ ๋ถ์ ์ ํ ๋ฒ์ญ์ด๋ผ๊ณ ์๊ฐ๋๋ค.
class Vehicle {
String brand;
Vehicle(this.brand);
void honk() {
print('Beep!');
}
}
class Car extends Vehicle {
String model;
Car(String brand, this.model) : super(brand); // super(brand)๋ก ๋ถ๋ชจํด๋์ค์ ์ ๋ฌ
void showDetails() {
print('Brand: $brand, Model: $model');
}
@override
void honk() {
super.honk();
print("This is Car!");
}
}
void main() {
var car = Car('Toyota', 'Camry');
car.honk();
car.showDetails();
}
9. Mixins
Mixin์ ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ธฐ ์ํด ์ฌ์ฉ๋๋ฉฐ, ์ฌ๋ฌ ํด๋์ค์ ๊ณตํต์ ์ธ ๊ธฐ๋ฅ์ ์ถ๊ฐํ ๋ ์ฌ์ฉ๋๋ค.
Mixin์ with
ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉฐ, ์ผ๋ฐ์ ์ผ๋ก ์ํ๋ฅผ ๊ฐ์ง์ง ์๋ ๋ฉ์๋์ ์งํฉ์ ์ ์ํ๋ค.
Dart์์ Mixin์ ์ถ์ ํด๋์ค์ ๋ฌ๋ฆฌ ๋ค์ค ์์์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค.
์ค์ต ๋ฌธ์ 1
Flyable
๋ฏน์ค์ธ์ ์์ฑํ๊ณ ,Bird
ํด๋์ค์ ์ ์ฉํ์ธ์.Bird
ํด๋์ค์์ ๋ ์ ์๋ ํ๋์ ๋ํ๋ด๋ ๋ฉ์๋์ ์ ์ด๋ฆ์ ์ถ๋ ฅํ๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ธ์.
mixin Flyable {
void fly() {
print('Flying...');
}
}
class Bird with Flyable {
String name;
Bird(this.name);
void chirp() {
print('$name is chirping!');
}
}
void main() {
var bird = Bird('Sparrow');
bird.chirp();
bird.fly();
}
10. fromJson
์ธ๋ถ API๋ฅผ ํตํด์ Json ํํ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊ณ ์ด๋ฅผ dart์ class๋ก ๊ตฌํํ๊ฒ ๋์ด ๋ฐ์ดํฐ๋ฅผ ํธ๋ค๋งํ๊ฒ ๋๋ค. ์ด๋ฅผ ์ํ ๊ธฐ์ด์ ์ธ ํ์ต์ ํด๋ณด์. named constructor๋ฅผ ๊ตฌํํด์ ์ฌ์ฉํ๋ฉด ๋๋ค.
class Player {
final String name;
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": "Sooyoung",
"team": "red",
"xp": 0,
},
{
"name": "Mike",
"team": "blue",
"xp": 0,
},
{
"name": "Darn",
"team": "red",
"xp": 0,
},
];
apiData.forEach((playerJson) {
var player = Player.fromJson(playerJson);
player.sayHello();
});
}
11. ์ข ํฉํ๊ฐ
์ค์ต ๋ฌธ์
- ๋๋ฌผ์ด๋ผ๋ ์ถ์ ํด๋์ค๋ฅผ ์์ฑํ์ฌ ๊ณตํต ๋ฉ์๋๋ฅผ ์ ์ํ์ธ์.(์ ์ด๋ฆ, ์๋ฆฌ)
- ์ด๋ฅผ ์์๋ฐ์ ๊ตฌ์ฒด์ ์ธ ํด๋์ค๋ฅผ ๊ตฌํํ๊ณ , ๋ฏน์ค์ธ์ ์ถ๊ฐํ์ฌ ์ถ๊ฐ ๊ธฐ๋ฅ์ ๊ตฌํํ์ธ์.(๋๊ณ ๋, swimmingโฆ)
abstract class Animal {
String name;
Animal(this.name);
void sound();
}
mixin Swim {
void swim() {
print('Swimming...');
}
}
class Dolphin extends Animal with Swim {
Dolphin(String name) : super(name);
@override
void sound() {
print('$name makes a clicking sound!');
}
}
void main() {
var dolphin = Dolphin('Dolly');
dolphin.sound();
dolphin.swim();
}