상속 받으면 부모 클래스의 모든 속성을 자식 클래스가 부여받음
void main() {
Idol blockB = Idol(name: "blockB", memberCnt: 7);
blockB.sayName();
blockB.sayMemberCnt();
print("-"*10);
BoyGroup bts = BoyGroup("bts", 7);
bts.sayName();
bts.sayMemberCnt();
bts.sayMale();
}
class Idol{
String name;
int memberCnt;
Idol({
required this.name,
required this.memberCnt,
});
void sayName(){
print("저희는 ${this.name}입니다.");}
void sayMemberCnt(){
print("${this.name}는 ${this.memberCnt}명의 멤버가 있습니다.");}
}
class BoyGroup extends Idol {
BoyGroup(
String name,
int memberCnt,
): super(
name: name,
memberCnt: memberCnt,);
void sayMale(){
print("저희는 남자 아이돌입니다.");
}
}
무조건 부모->자식
자식 클래스에서 선언한 것들은 부모 클래스에서는 사용할 수 없음
같은 자식 클래스끼리도 속성들을 공유하지 않음
method overriding
method: 클래스 내부에 있는 함수
override: 우선시하다
void main() {
TimesTwo timesTwo = TimesTwo(2);
print(timesTwo.calc());
TimesFour timesFour = TimesFour(2);
print(timesFour.calc());
}
class TimesTwo{
final int number;
TimesTwo(
this.number,);
// method
int calc(){
return number * 2;
}
}
class TimesFour extends TimesTwo{
TimesFour(
int number,) : super(number);
@override
int calc(){
return super.number * 4;
}
}
static
static은 instance에 귀속되지 않고 class에 귀속됨
void main() {
Employee dami = Employee("다미");
Employee chacha = Employee("차차");
Employee zico = Employee("지코");
dami.printNameAndBuilding();
chacha.printNameAndBuilding();
zico.printNameAndBuilding();
print("-"*20);
Employee.building = "Dami Tower";
dami.printNameAndBuilding();
chacha.printNameAndBuilding();
zico.printNameAndBuilding();
}
class Employee{
static String? building; // 직원이 일하고 있는 건물
String name; // 직원 이름
Employee(
this.name,);
void printNameAndBuilding(){
print("제 이름은 ${this.name}입니다. ${Employee.building}에서 일하고 있습니다.");
}
}
interface: 어떤 특수한 구조를 강제
void main() {
BoyGroup bts = BoyGroup("bts");
GirlGroup apink = GirlGroup("apink");
bts.sayName();
apink.sayName();
}
//interface
class IdolInterface{
String name;
IdolInterface(this.name);
void sayName(){}
}
class BoyGroup implements IdolInterface{
String name;
BoyGroup(this.name);
void sayName(){
print("제 이름은 ${name}입니다.");
}
}
class GirlGroup implements IdolInterface{
String name;
GirlGroup(this.name);
void sayName(){
print("제 이름은 ${name}입니다.");
}
}
abstract가 있으면 얘는 interface 만들 때 선언하지 말라는 뜻
void main() {
BoyGroup bts = BoyGroup("bts");
GirlGroup apink = GirlGroup("apink");
bts.sayName();
apink.sayName();
}
// interface
// abstract가 있으면 얘는 interface 만들 때 선언하지 말라는 뜻
abstract class IdolInterface{
String name;
IdolInterface(this.name);
void sayName();
}
class BoyGroup implements IdolInterface{
String name;
BoyGroup(this.name);
void sayName(){
print("제 이름은 ${name}입니다.");
}
}
class GirlGroup implements IdolInterface{
String name;
GirlGroup(this.name);
void sayName(){
print("제 이름은 ${name}입니다.");
}
}
generic: type을 외부에서 받을 때 사용
void main() {
Lecture<String> lecture1 = Lecture("123", "Lecture1");
lecture1.printIdType();
Lecture<int> lecture2 = Lecture(123, "Lecture2");
lecture2.printIdType();
}
class Lecture<T>{
final T id;
final String name;
Lecture(this.id, this.name);
void printIdType(){
print(id.runtimeType);
}
}
void main() {
Lecture<String, String> lecture1 = Lecture("123", "Lecture1");
lecture1.printIdType();
Lecture<int, String> lecture2 = Lecture(123, "Lecture2");
lecture2.printIdType();
}
class Lecture<T, X>{
final T id;
final X name;
Lecture(this.id, this.name);
void printIdType(){
print(id.runtimeType);
}
}
이런 식으로 <T, X> 등 계속 넣어 사용 가능
모든 클래스들이 최상위 object 클래스에서 상속받기 때문에 OOP라고 부름
'CS > Flutter | Dart' 카테고리의 다른 글
Dart cascading operator, functional programming (0) | 2023.07.12 |
---|---|
Dart Functional Programming - map, set, reduce, fold (1) | 2023.07.10 |
Dart OOP - class, instance, getter, setter, private (1) | 2023.07.09 |
Dart 기본 - if, for, while, function, parameter, typedef (0) | 2023.07.09 |
Dart 기본 - 변수형 (0) | 2023.07.07 |