#객체지향프로그래밍 #Dart #입문
void main() {
Idol blackPink = Idol();
print(blackPink.name);
print(blackPink.members);
blackPink.sayHello();
blackPink.introduce();
}
// Idol class
// name (이름) - 변수
// members(멤버들) - 변수
// sayHello (인사) - 함수
// introduce (멤버소개) - 함수
class Idol {
String name = '블랙핑크';
List<String> members = ['지수', '제니', '리사', '로제'];
void sayHello(){
print('안녕하세요 블랙핑크입니다');
}
void introduce(){
print('저희 멤버는 지수,제니,리사,로제가 있습니다');
}
}
void main() {
Idol blackPink = Idol('블랙핑크', ['지수', '제니', '리사', '로제']);
print(blackPink.name);
print(blackPink.members);
blackPink.sayHello();
blackPink.introduce();
Idol bts = Idol.fromList(
[
['RM', '진', '슈가', '제이홉', '지민', '뷔', '정국'], 'bts'
]);
print(bts.name);
print(bts.members);
bts.sayHello();
bts.introduce();
}
// Idol class
// name (이름) - 변수
// members(멤버들) - 변수
// sayHello (인사) - 함수
// introduce (멤버소개) - 함수
class Idol {
String name;
List<String> members;
// Idol(String name, List<String> members)
// : this.name = name,
// this.members = members;
Idol(this.name, this.members);
Idol.fromList(List value)
: this.members = value[0],
this.name = value[1];
void sayHello() {
print('안녕하세요 ${this.name}입니다');
}
void introduce() {
print('저희 멤버는 ${this.members}가 있습니다');
}
}
class Idol {
final String name;
final List<String> members;
const Idol(this.name, this.members);
Idol.fromList(List value)
: this.members = value[0],
this.name = value[1];
// getter
String get firstMember {
return this.members[0];
}
// setter 는 하나만 넣을 수 있다. 사용하는 측을 보면 이해할 수 있다.
set firstMember(String name ) {
this.member[0] = name
}
// blackPink.firstMember = 'xxx';
}
만약 private 로 해야 한다면 '_ ' 을 붙인다.
상속 기능을 사용한다면 다음과 같이한다.
void main() {
print('---------idol--------');
Idol apink = Idol(name: '에이핑크', memberCount: 5);
apink.sayMemberCount();
apink.sayName();
print('---------boy group idol--------');
BoyGroup bts = BoyGroup('BTS', 7);
bts.sayMemberCount();
bts.sayName();
print('---------girl group idol--------');
GirlGroup redVelvet = GirlGroup('redVelvet', 5);
redVelvet.sayMemberCount();
redVelvet.sayName();
print('---------Type Comparison--------');
print(bts is Idol);
print(bts is BoyGroup);
print(bts is GirlGroup);
print('---------Type Comparison--------');
print(apink is Idol);
print(apink is BoyGroup);
print(apink is GirlGroup);
print('---------Type Comparison--------');
print(redVelvet is Idol);
print(redVelvet is BoyGroup);
print(redVelvet is GirlGroup);
}
// 상속 - inheritance
//
// 상속을 받으면 부모 클래스의 모든 속성을 자식 클래스가 부여받는다.
class Idol {
String name;
int memberCount;
Idol({
required this.name,
required this.memberCount,
});
void sayName() {
print('저는 ${this.name} 입니다.');
}
void sayMemberCount() {
print('${this.name}은 ${this.memberCount}명의 멤버가 있습니다.');
}
}
class BoyGroup extends Idol {
BoyGroup(
String name,
int membersCount,
) : super(name: name, memberCount: membersCount);
}
class GirlGroup extends Idol {
GirlGroup(
String name,
int membersCount,
) : super(name: name, memberCount: membersCount);
}
---------idol--------
에이핑크은 5명의 멤버가 있습니다.
저는 에이핑크 입니다.
---------boy group idol--------
BTS은 7명의 멤버가 있습니다.
저는 BTS 입니다.
---------girl group idol--------
redVelvet은 5명의 멤버가 있습니다.
저는 redVelvet 입니다.
---------Type Comparison--------
true
true
false
---------Type Comparison--------
true
false
false
---------Type Comparison--------
true
false
true
method
void main() {
TimesTwo tt = TimesTwo(2);
print(tt.calculate());
}
// method - function (class 내부에 있는 함수)
// orverride - 덮어쓰다 (우선시하다)
class TimesTwo {
final int number;
TimesTwo(this.number,
);
// method
int calculate(){
return number * 2;
}
}
override
void main() {
TimesTwo tt = TimesTwo(2);
print(tt.calculate());
TimesFour tt2 = TimesFour(2);
print(tt2.calculate());
}
// method - function (class 내부에 있는 함수)
// orverride - 덮어쓰다 (우선시하다)
class TimesTwo {
final int number;
TimesTwo(
this.number,
);
// method
int calculate() {
return number * 2;
}
}
class TimesFour extends TimesTwo {
final int number;
TimesFour(
this.number,
) : super(number);
// method
@override
int calculate() {
return super.calculate() * 2;
}
}
static 을 어떻게 사용할 수 있을까?
void main() {
Employee a = Employee('슬기');
Employee b = Employee('초롱');
a.name ='코드팩토리';
a.printNameAndBuilding();
b.printNameAndBuilding();
Employee.building = '트레바리!';
a.printNameAndBuilding();
b.printNameAndBuilding();
Employee.printBuilding();
}
class Employee {
// static 은 instance에 귀속되지 않고 class 에 귀속된다.
// 알바생이 일하고있는 건물
static String? building;
String name;
Employee(this.name);
void printNameAndBuilding(){
print('제 이름은 $name 입니다. $building 건물에서 근무하고 있습니다');
}
static void printBuilding(){
print('저는 $building 건물에서 근무중입니다.');
}
}
interface
void main() {
BoyGroup bts = BoyGroup('BTS');
bts.sayName();
GirlGroup redVelvet = GirlGroup('레드벨벳');
redVelvet.sayName();
}
//interface
abstract class IdolInterfaceAbs {
String name;
IdolInterfaceAbs(this.name);
void sayName();
}
class IdolInterface {
String name;
IdolInterface(this.name);
void sayName(){}
}
class BoyGroup implements IdolInterfaceAbs {
String name;
BoyGroup(this.name);
void sayName() {
print('제 이름은 $name 입니다.');
}
}
class GirlGroup implements IdolInterfaceAbs {
String name;
GirlGroup(this.name);
void sayName() {
print('제 이름은 $name 입니다.');
}
}
generic
void main() {
Lecture<String> lecture = Lecture('123', '123123');
lecture.printIdType();
}
// generic - 타입을 외부에서 받을때 사용
class Lecture<T> {
final T id;
final String name;
Lecture(this.id, this.name);
void printIdType() {
print(id.runtimeType);
}
}
%% - [[1. note/study/dart/#4 비동기프로그래밍]] %% %% - [[1. note/study/dart/#3 함수형프로그래밍]] %%