Search

객체지ν–₯

객체지ν–₯

1.
클래슀
2.
객체
3.
μƒμ„±μž
4.
상속
5.
좔상 클래슀
6.
믹슀인
7.
enum
8.
static

클래슀

객체 μ •μ˜ν•˜λŠ” 섀계도
// Person 클래슀 μ •μ˜ class Person { // 멀버 λ³€μˆ˜ (속성) String name; int age; // μƒμ„±μž Person(this.name, this.age); // 멀버 λ©”μ„œλ“œ void introduceYourself() { print('Hello, my name is $name and I am $age years old.'); } } void main() { // Person 클래슀의 μΈμŠ€ν„΄μŠ€ 생성 var person1 = Person('Alice', 25); // μΈμŠ€ν„΄μŠ€μ˜ 속성에 μ ‘κ·Ό print('Name: ${person1.name}, Age: ${person1.age}'); // μΈμŠ€ν„΄μŠ€μ˜ λ©”μ„œλ“œ 호좜 person1.introduceYourself(); // 좜λ ₯: Hello, my name is Alice and I am 25 years old. // 또 λ‹€λ₯Έ μΈμŠ€ν„΄μŠ€ 생성 var person2 = Person('Bob', 30); person2.introduceYourself(); // 좜λ ₯: Hello, my name is Bob and I am 30 years old. }
Dart
볡사

객체

μ‹€μ„Έκ³„μ˜ 속성과 κΈ°λŠ₯을, ν”„λ‘œκ·Έλž¨μ˜ λ³€μˆ˜μ™€ λ©”μ†Œλ“œλ‘œ μΆ”μƒν™”ν•œ 것

객체 생성

var 객체λͺ… = 객체λͺ…(인자1, 인자2);
Dart
볡사
β€’
μ˜ˆμ‹œμ½”λ“œ
void main() { // Person 클래슀의 μΈμŠ€ν„΄μŠ€ 생성 var person1 = Person('Alice', 25); // 또 λ‹€λ₯Έ μΈμŠ€ν„΄μŠ€ 생성 var person2 = Person('Bob', 30); // 객체의 속성에 μ ‘κ·Ό print('Person 1: Name - ${person1.name}, Age - ${person1.age}'); print('Person 2: Name - ${person2.name}, Age - ${person2.age}'); // 객체의 λ©”μ„œλ“œ 호좜 person1.introduceYourself(); // 좜λ ₯: Hello, my name is Alice and I am 25 years old. person2.introduceYourself(); // 좜λ ₯: Hello, my name is Bob and I am 30 years old. }
Dart
볡사

μƒμ„±μž

객체λ₯Ό 생성할 λ•Œ, λ³€μˆ˜ λ˜λŠ” λ©”μ†Œλ“œλ₯Ό μ΄ˆκΈ°ν™”ν•˜κΈ° μœ„ν•΄μ„œ μ‹€ν–‰λ˜λŠ” λ©”μ†Œλ“œ
객체λ₯Ό μ΄ˆκΈ°ν™”ν•˜λŠ” νŠΉμˆ˜ν•œ λ©”μ„œλ“œ
클래슀의 이름과 λ™μΌν•œ 이름을 가지며, λ§€κ°œλ³€μˆ˜λ₯Ό κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.
ν΄λž˜μŠ€μ— λͺ…μ‹œμ μΈ μƒμ„±μžκ°€ μ—†λŠ” 경우 κΈ°λ³Έ μƒμ„±μžκ°€ μžλ™μœΌλ‘œ μƒμ„±λ©λ‹ˆλ‹€.
class Person { String name; int age; Person(this.name, this.age); // μƒμ„±μž void sayHello() { print('Hello, $name!'); } } void main() { var person = Person('John', 25); person.sayHello(); // 좜λ ₯: Hello, John! }
Dart
볡사

좔상 클래슀

클래슀의 계측적인 섀계λ₯Ό μœ„ν•΄, κ΅¬ν˜„ μ½”λ“œκ°€ μ—†λŠ” 좔상 λ©”μ†Œλ“œλ‘œ κ΅¬μ„±λœ 클래슀

좔상 클래슀 ν‚€μ›Œλ“œ

abstract
Dart
볡사
abstract class Shape { double getArea(); // 좔상 λ©”μ„œλ“œ } class Circle extends Shape { double radius; Circle(this.radius); double getArea() { return 3.14 * radius * radius; } }
Dart
볡사
좔상 클래슀 ν‚€μ›Œλ“œλŠ” ν΄λž˜μŠ€λΏλ§Œμ•„λ‹ˆλΌ λ―ΉμŠ€μΈμ—λ„ μ μš©κ°€λŠ₯ν•˜λ‹€.
abstract mixin class Musician { // No 'on' clause, but an abstract method that other types must define if // they want to use (mix in or extend) Musician: void playInstrument(String instrumentName); void playPiano() { playInstrument('Piano'); } void playFlute() { playInstrument('Flute'); } } class Virtuoso with Musician { // Use Musician as a mixin void playInstrument(String instrumentName) { print('Plays the $instrumentName beautifully'); } } class Novice extends Musician { // Use Musician as a class void playInstrument(String instrumentName) { print('Plays the $instrumentName poorly'); } }
Dart
볡사

상속

λΆ€λͺ¨ 클래슀의 λ³€μˆ˜μ™€ λ©”μ†Œλ“œλ₯Ό μžμ‹ ν΄λž˜μŠ€μ—μ„œ μž¬μ‚¬μš©ν•˜λŠ” 것
class Animal { void makeSound() { print('Some generic sound'); } } class Dog extends Animal { void makeSound() { print('Woof! Woof!'); } } void main() { var dog = Dog(); dog.makeSound(); // 좜λ ₯: Woof! Woof! }
Dart
볡사

믹슀인(Minin)

닀쀑 상속을 μ§€μ›ν•˜λŠ” 기법
mixin Flying { void fly() { print('Flying high!'); } } class Bird with Flying { // Bird ν΄λž˜μŠ€μ— Flying 믹슀인 μΆ”κ°€ } void main() { var bird = Bird(); bird.fly(); // 좜λ ₯: Flying high! }
Dart
볡사
// Flying 믹슀인 mixin Flying { void fly() { print('Flying high!'); } } // Swimming 믹슀인 mixin Swimming { void swim() { print('Swimming gracefully!'); } } // Bird ν΄λž˜μŠ€μ— Flying 믹슀인 적용 class Bird with Flying { String name; Bird(this.name); } // Fish ν΄λž˜μŠ€μ— Swimming 믹슀인 적용 class Fish with Swimming { String name; Fish(this.name); } // Duck ν΄λž˜μŠ€μ— Flyingκ³Ό Swimming 믹슀인 적용 (닀쀑 상속) class Duck with Flying, Swimming { String name; Duck(this.name); } void main() { var eagle = Bird('Eagle'); eagle.fly(); // 좜λ ₯: Flying high! var salmon = Fish('Salmon'); salmon.swim(); // 좜λ ₯: Swimming gracefully! var mallard = Duck('Mallard'); mallard.fly(); // 좜λ ₯: Flying high! mallard.swim(); // 좜λ ₯: Swimming gracefully! }
Dart
볡사
λ―ΉμŠ€μΈμ— λŒ€ν•˜μ—¬λ„ λ©”μ†Œλ“œ μ˜€λ²„λΌμ΄λ”©μ΄ κ°€λŠ₯ν•˜λ‹€.
// Flying 믹슀인 mixin Flying { void fly() { print('Default flying behavior'); } } // Bird ν΄λž˜μŠ€μ— Flying 믹슀인 적용 class Bird with Flying { String name; Bird(this.name); // Flying 믹슀인의 fly λ©”μ„œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”© void fly() { print('$name is flying high!'); } } void main() { var eagle = Bird('Eagle'); eagle.fly(); // 좜λ ₯: Eagle is flying high! }
Dart
볡사

enum

μƒμˆ˜λ“€μ„ μ—΄κ±°ν•˜μ—¬ 묢어놓은 νƒ€μž…
enum Weekday { monday, tuesday, wednesday, thursday, friday } void main() { var today = Weekday.wednesday; print(today); // 좜λ ₯: Weekday.wednesday }
Dart
볡사

static

λ³€μˆ˜ λ˜λŠ” λ©”μ†Œλ“œλ₯Ό ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ μ‹œ, κ°€μž₯ λ¨Όμ € λ©”λͺ¨λ¦¬μ— κ³ μ •ν•˜λŠ” ν‚€μ›Œλ“œ
class Calculator { static double add(double a, double b) { return a + b; } } void main() { var result = Calculator.add(5.0, 3.0); print(result); // 좜λ ₯: 8.0 }
Dart
볡사