1. ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ (OOP)
1.1 ํด๋์ค์ ์ธ์คํด์ค
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ (Object Oriented Programming; OOP)
ํด๋์ค(class): ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ธฐ ์ํ ์ค๊ณ๋์ธ์คํด์ค(instance): ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ ๋ง๋ค์ด์ง ๊ฐ์ฒด- e.g. ํด๋์ค - ์ถ๊ตฌํ(์ ์, ๊ฐ๋ , ํ๋ช )
- e.g. ์ธ์คํด์ค - ์ํฅ๋ฏผ, ๋ฌด๋ฆฌ๋ด, ํ ํธ๋ ํ์คํผ
1void main() {2Team barca = Team(3'๋ฐ๋ฅด์ค',4['๋ฉ์', '์์๋ ์ฆ', '๋ค์ด๋ง๋ฅด']5);67print(barca.name);8print(barca.members);9barca.sayHello();10barca.introduce();1112Team real = Team(13'๋ ์',14['ํธ๋ ๋', '์นด์์ผ์ค', '๋ผ๋ชจ์ค']15);16print(real.name);17print(real.members);18real.sayHello();19real.introduce();20}2122class Team {23String name;24List<String> members;2526// ์์ฑ์(constructor) : ํด๋์ค๊ฐ ์ธ์คํด์คํ ๋ ๋ ํธ์ถ๋๋ ํจ์27Team(String name, List<String> members) :28this.name = name,29this.members = members;303132void sayHello( ) {33print('์๋ ํ์ธ์, ${this.name}์ ๋๋ค.');34}3536void introduce( ) {37print('์ฐ๋ฆฌ ํ์ ์ ์๋ค์ ${this.members}์ ๋๋ค.');38}39}
1.2 ์์ฑ์(constructor) ์ถ์ฝํ
์์ฑ์๋ฅผ ๋ ๊ฐ๋จํ๊ฒ ํํํ ์ ์๋ค.
1// ์์ฑ์(constructor) : ํด๋์ค๊ฐ ์ธ์คํด์คํ ๋ ๋ ํธ์ถ๋๋ ํจ์2Team(String name, List<String> members) :3this.name = name,4this.members = members;56Team(this.name, this.members); // ์์ ๋์ผํ ์ฝ๋ (์์ฑ์ ์ถ์ฝํ)
1.3 named constructor
์์ฑ์๋ฅผ ์ฌ๋ฌ ๊ฐ ๋ง๋ค ์ ์๋ค.
1void main() {2Team barca = Team('๋ฐ๋ฅด์ค', ['๋ฉ์', '์์๋ ์ฆ', '๋ค์ด๋ง๋ฅด']);34print(barca.name);5print(barca.members);6barca.sayHello();7barca.introduce();89Team real = Team.fromList([10['ํธ๋ ๋', '์นด์์ผ์ค', '๋ผ๋ชจ์ค'],11'๋ ์',12]);13print(real.name);14print(real.members);15real.sayHello();16real.introduce();17}1819class Team {20String name;21List<String> members;2223// ์์ฑ์(constructor) : ํด๋์ค๊ฐ ์ธ์คํด์คํ ๋ ๋ ํธ์ถ๋๋ ํจ์24Team(this.name, this.members);2526// ๐ ์ด๋ฆ์๋ ์์ฑ์(named constructor) : ์์ฑ์๋ฅผ ์ฌ๋ฌ๊ฐ ๋ง๋ค ์ ์์27Team.fromList(List values)28: this.members = values[0],29this.name = values[1];3031void sayHello() {32print('์๋ ํ์ธ์, ${this.name}์ ๋๋ค.');33}3435void introduce() {36print('์ฐ๋ฆฌ ํ์ ์ ์๋ค์ ${this.members}์ ๋๋ค.');37}38}39
1.4 final๊ณผ const
final ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ณ์๋ฅผ ์์๋ก ๋ง๋ค ์ ์๋ค.
โconstโ ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด, ์ปดํ์ผ ์๊ฐ ์์๋ก ๋ง๋ค ์ ์๋ค.
1void main() {2// ๐ const๋ก ์ ์ธ๋ ๊ฐ์ฒด๋ ๋ถ๋ณ ๊ฐ์ฒด3Team barca = const Team('๋ฐ๋ฅด์ค', ['๋ฉ์', '์์๋ ์ฆ', '๋ค์ด๋ง๋ฅด']);45print(barca.name);6print(barca.members);7barca.sayHello();8barca.introduce();910Team real = Team.fromList([11['ํธ๋ ๋', '์นด์์ผ์ค', '๋ผ๋ชจ์ค'],12'๋ ์',13]);14print(real.name);15print(real.members);16real.sayHello();17real.introduce();18}1920class Team {21// ๐ final : ํ๋ฒ ์ค์ ๋๋ฉด ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ22final String name;23final List<String> members;2425// ์์ฑ์(constructor) : ํด๋์ค๊ฐ ์ธ์คํด์คํ ๋ ๋ ํธ์ถ๋๋ ํจ์26// ๐ const : ๊ฐ์ฒด๋ฅผ ๋ถ๋ณ ์์ฑ์๋ก ๋ง๋ค์ด์ค27const Team(this.name, this.members);2829// ๐ ์ด๋ฆ์๋ ์์ฑ์(named constructor) : ์์ฑ์๋ฅผ ์ฌ๋ฌ๊ฐ ๋ง๋ค ์ ์์30Team.fromList(List values)31: this.members = values[0],32this.name = values[1];3334void sayHello() {35print('์๋ ํ์ธ์, ${this.name}์ ๋๋ค.');36}3738void introduce() {39print('์ฐ๋ฆฌ ํ์ ์ ์๋ค์ ${this.members}์ ๋๋ค.');40}41}
1.5 ์์ฑ๋ ์ธ์คํด์ค ๋น๊ต (feat. const)
์ธ์คํด์ค๋ฅผ ์์ฑํ๋ฉด, ์ธ์คํด์ค๊ฐ ์ปดํจํฐ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ๋๋ฐ, ์์ฑํ ๋๋ง๋ค ์๋กญ๊ฒ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ๋ค. ๊ทธ๋์ ์๋ฌด๋ฆฌ ๊ฐ์ด ๋๊ฐ๋๋ผ๋, ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ๋ ์ฃผ์๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ๊ฐ์์ง ๋น๊ตํ๋ฉด false๊ฐ ๋์จ๋ค.
1void main() {2Team barca = Team('๋ฐ๋ฅด์ค', ['๋ฉ์', '์์๋ ์ฆ', '๋ค์ด๋ง๋ฅด']);3Team barca2 = Team('๋ฐ๋ฅด์ค', ['๋ฉ์', '์์๋ ์ฆ', '๋ค์ด๋ง๋ฅด']);45print(barca == barca2); // false6}78class Team {9final String name;10final List<String> members;1112// ์์ฑ์(constructor) : ํด๋์ค๊ฐ ์ธ์คํด์คํ ๋ ๋ ํธ์ถ๋๋ ํจ์13const Team(this.name, this.members);14}
๋ค๋ง, ๋ ๋ค const๋ก ์ ์ธํ๊ฒ ๋๋ฉด, ๊ฐ์ ์ธ์คํด์ค๋ก ์ธ์ํ๋ค.
1void main() {2Team barca = const Team('๋ฐ๋ฅด์ค', ['๋ฉ์', '์์๋ ์ฆ', '๋ค์ด๋ง๋ฅด']);3Team barca2 = const Team('๋ฐ๋ฅด์ค', ['๋ฉ์', '์์๋ ์ฆ', '๋ค์ด๋ง๋ฅด']);45print(barca == barca2); // true6}78class Team {9final String name;10final List<String> members;1112// ์์ฑ์(constructor) : ํด๋์ค๊ฐ ์ธ์คํด์คํ ๋ ๋ ํธ์ถ๋๋ ํจ์13const Team(this.name, this.members);14}
2. getter์ setter
getter: ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ๋ ์ฌ์ฉํ๋ ํจ์setter: ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ ๋ ์ฌ์ฉํ๋ ํจ์
1void main() {2Team barca = Team('๋ฐ๋ฅด์ค', ['๋ฉ์', '์์๋ ์ฆ', '๋ค์ด๋ง๋ฅด']);34Team real = Team.fromList([5['ํธ๋ ๋', '์นด์์ผ์ค', '๋ผ๋ชจ์ค'],6'๋ ์',7]);89print(barca.firstMember); // ๋ฉ์10print(real.firstMember); // ํธ๋ ๋1112barca.firstMember = 'ํผ์ผ';13real.firstMember = '๋ฒ ์ผ';1415print(barca.firstMember); // ํผ์ผ16print(real.firstMember); // ๋ฒ ์ผ17}1819class Team {20String name;21List<String> members;2223// ์์ฑ์(constructor) : ํด๋์ค๊ฐ ์ธ์คํด์คํ ๋ ๋ ํธ์ถ๋๋ ํจ์24Team(this.name, this.members);2526Team.fromList(List values)27: this.members = values[0],28this.name = values[1];2930// getter31String get firstMember {32return this.members[0];33}3435// setter : ๋ฌด์กฐ๊ฑด 1๊ฐ์ ์ธ์๋ง ๋ฐ์์ผ ํจ36set firstMember(String name) {37this.members[0] = name;38}39}
2.1 ์ getter์ setter๋ฅผ ์ฌ์ฉํ๋?
getter์setter๋ฅผ ์ฌ์ฉํ๋ฉด,- ํด๋์ค ๋ด๋ถ์ ๋ณ์๋ฅผ ์ธ๋ถ์์ ์ง์ ์ ์ผ๋ก ์ ๊ทผํ์ง ์๊ณ , ํจ์๋ฅผ ํตํด ์ ๊ทผํ ์ ์๋ค.
- final ๋ณ์์ ๋ํด์๋ setter๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- ๋ค๋ง, ํ๋ ํ๋ก๊ทธ๋๋ฐ์์ setter๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ์ง ์๋๋ค.
_๋ฅผ ๋ณ์๋ช , ํจ์๋ช ์์ ๋ถ์ด๋ฉด, private์ผ๋ก ๋ง๋ค ์ ์๋ค.
1void main() {2_Team barca = _Team('๋ฐ๋ฅด์ค', ['๋ฉ์', '์์๋ ์ฆ', '๋ค์ด๋ง๋ฅด']);34_Team real = _Team.fromList([5['ํธ๋ ๋', '์นด์์ผ์ค', '๋ผ๋ชจ์ค'],6'๋ ์',7]);89print(barca.firstMember); // ๋ฉ์10print(real.firstMember); // ํธ๋ ๋1112barca.firstMember = 'ํผ์ผ';13real.firstMember = '๋ฒ ์ผ';1415print(barca.firstMember); // ํผ์ผ16print(real.firstMember); // ๋ฒ ์ผ17}1819class _Team {20final String name;21final List<String> members;2223// ์์ฑ์(constructor) : ํด๋์ค๊ฐ ์ธ์คํด์คํ ๋ ๋ ํธ์ถ๋๋ ํจ์24_Team(this.name, this.members);2526_Team.fromList(List values)27: this.members = values[0],28this.name = values[1];2930// getter : ๋ฐ์ดํฐ๋ฅผ ๊ฐ๋จํ๊ฒ ๊ฐ๊ณตํด์ ๋ฐํํ ๋ ์ฌ์ฉ31String get firstMember {32return this.members[0];33}3435// setter : ๋ฌด์กฐ๊ฑด 1๊ฐ์ ์ธ์๋ง ๋ฐ์์ผ ํจ36set firstMember(String name) {37this.members[0] = name;38}39}
3. ์์ (Inheritance)
์์: ๋ถ๋ชจ ํด๋์ค์ ํน์ฑ์ ์์ ํด๋์ค๊ฐ ๋ฌผ๋ ค๋ฐ๋ ๊ฒextendsํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์์์ ๋ฐ์ ์ ์๋ค.
1void main() {2print('--------------ํ----------------');3Team barca = Team(name: '๋ฐ๋ฅด์ค', membersCount: 25);4barca.sayName();5barca.sayMembersCount();67print('--------------์ผ๊ตฌํ------------');8BaseballTeam samsung = BaseballTeam(name: '์ผ์ฑ๋ผ์ด์จ์ฆ', membersCount: 30);9samsung.sayName();10samsung.sayMembersCount();11samsung.sayBaseballTeamName();1213print('--------------๋๊ตฌํ------------');14BasketballTeam bulls = BasketballTeam(name: '์์นด๊ณ ๋ถ์ค', membersCount: 15);15bulls.sayName();16bulls.sayMembersCount();17bulls.sayBasketballTeamName();1819print('--------------ํ์ ๋น๊ต------------');20print(barca is Team); // true21print(barca is BaseballTeam); // false22print(barca is BasketballTeam); // false2324print('--------------ํ์ ๋น๊ต2------------');25print(samsung is Team); // true26print(samsung is BaseballTeam); // true27print(samsung is BasketballTeam); // false28}2930class Team {31String name;32int membersCount;3334Team({35required this.name,36required this.membersCount,37});3839void sayName() {40print('ํ๋ช : $name');41}4243void sayMembersCount() {44print('ํ์ ์ : $membersCount');45}46}4748class BaseballTeam extends Team {49// super() : ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถ50BaseballTeam({51required String name,52required int membersCount,53}) : super(name: name, membersCount: membersCount);5455sayBaseballTeamName() {56print('์ผ๊ตฌํ์์๋ง ์ฐ๋ ๋ฉํธ');57}58}5960class BasketballTeam extends Team {61BasketballTeam({62required String name,63required int membersCount,64}) : super(name: name, membersCount: membersCount);6566sayBasketballTeamName() {67print('๋๊ตฌํ์์๋ง ์ฐ๋ ๋ฉํธ');68}69}
4. ๋ฉ์๋(method)
๋ฉ์๋(method): ํด๋์ค ๋ด๋ถ์ ์๋ ํจ์์ค๋ฒ๋ผ์ด๋(override, ๋ฎ์ด์ฐ๊ธฐ): ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ์์ ํด๋์ค์์ ์ฌ์ ์ํ๋ ๊ฒ
1void main() {2TimesTwo timesTwo = TimesTwo(2);3print(timesTwo.calculate()); // 445TimesFour timesFour = TimesFour(2);6print(timesFour.calculate()); // 87}89class TimesTwo {10final int number;1112TimesTwo(13this.number14);1516// method17int calculate() {18return number * 2;19}20}2122class TimesFour extends TimesTwo {23TimesFour(int number) : super(number);2425@override // ์ค๋ฒ๋ผ์ด๋ฉ annotation(์ฃผ์)26int calculate() {27return super.number * 4;28// return super.calculate() * 2; // ์ ์ฝ๋์ ๋์ผํ ๊ฒฐ๊ณผ29}30}
4.1 static ํค์๋
1void main() {2Employee seulgi = Employee('์ฌ๊ธฐ');3Employee chorong = Employee('์ด๋กฑ');45seulgi.name = '๋ญ์๊ธฐ';6seulgi.printNameAndBuilding();7chorong.printNameAndBuilding();89// ์ธ์คํด์ค๊ฐ ์๋ class์ ๊ท์๋์ด์,10// ๋ชจ๋ ์ธ์คํด์ค์์, ํด๋์ค์ ๊ฑด๋ฌผ ์ด๋ฆ์ ๊ณต์ ํ๋ค.11Employee.building = '์คํฌํ์';12seulgi.printNameAndBuilding();13chorong.printNameAndBuilding();14}1516class Employee {17// static๋ฅผ ๋ถ์ด๋ฉด, instance์ ๊ท์๋์ง ์๊ณ , class์ ๊ท์๋๋ค.18static String? building; // ์๋ฐ์์ด ์ผํ๊ณ ์๋ ๊ฑด๋ฌผ19String name; // ์๋ฐ์ ์ด๋ฆ2021Employee(this.name);2223void printNameAndBuilding() {24print('์ด๋ฆ: $name, ๊ฑด๋ฌผ: $building');25}2627static void printBuilding() {28print('๊ฑด๋ฌผ: $building');29}30}
4.2 interface ํค์๋ (feat. implements)
interface: ํด๋์ค๊ฐ ๊ตฌํํด์ผ ํ๋ ๋ณ์, ๋ฉ์๋๋ฅผ ์ ์ํ๋ ๊ฒ
1void main() {2BoyGroup bts = BoyGroup('BTS');3GirlGroup twice = GirlGroup('TWICE');45bts.sayName();6twice.sayName();7}89class IdolInterface {10String name;1112IdolInterface(this.name);1314void sayName() {}15}1617class BoyGroup implements IdolInterface {18String name;1920BoyGroup(this.name);2122void sayName() {23print('๊ทธ๋ฃน๋ช ์ $name');24}25}2627class GirlGroup implements IdolInterface {28String name;2930GirlGroup(this.name);3132void sayName() {33print('๊ทธ๋ฃน๋ช ์ $name');34}35}
4.3 abstract ํค์๋ (feat. interface)
- ์ถ์ ํด๋์ค๋ฅผ ์ ์ํ๋ ๋ฐ ์ฌ์ฉ
- ์ถ์ ํด๋์ค๋ ์ฃผ๋ก ์ธํฐํ์ด์ค ์ญํ ์ ํ๋ฉฐ,
- ์ด๋ฅผ ์์๋ฐ๋ ํ์ ํด๋์ค์์ ํน์ ๋ฉ์๋๋ฅผ ๋ฐ๋์ ๊ตฌํํ๋๋ก ๊ฐ์ ํ๋ ์ญํ ์ ํฉ๋๋ค
- ์ฆ, abstract๊ฐ ์จ์ ธ ์์ผ๋ฉด ์ธ์คํด์ค๋ก ๋ง๋ค ๋, ํด๋น ๋ณ์, ๋ฉ์๋๋ง ์ฐ๋ผ๋ ๋ป
1void main() {2BoyGroup bts = BoyGroup('BTS');3GirlGroup twice = GirlGroup('TWICE');45bts.sayName();6twice.sayName();78print(bts is IdolInterface); // true9print(bts is BodyGroup); // true10print(bts is GirlGroup); // false1112print(twice is IdolInterface); // true13print(twice is BodyGroup); // false14print(twice is GirlGroup); // true15}1617abstract class IdolInterface {18String name;1920IdolInterface(this.name);2122// ์ถ์์ (abstract)์ด๊ธฐ ๋๋ฌธ์ ๋ฉ์๋์ body๋ฅผ ์ง์๋ ๋จ23void sayName()24}2526class BoyGroup implements IdolInterface {27String name;2829BoyGroup(this.name);3031void sayName() {32print('๊ทธ๋ฃน๋ช ์ $name');33}34}3536class GirlGroup implements IdolInterface {37String name;3839GirlGroup(this.name);4041void sayName() {42print('๊ทธ๋ฃน๋ช ์ $name');43}44}
5. generic
์ ๋ค๋ฆญ(generic): ํด๋์ค๋ ํจ์๋ฅผ ์ ์ํ ๋, ํ์ ์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์ ์ฌ์ฉํ๋ ๊ฒ- ์ฆ, ํ์ ์ ์ธ๋ถ์์ ๋ฐ์์ ์ฌ์ฉํ ๋, ์ฌ์ฉ
1void main() {2Lecture<String, String> lec1 = Lecture('123', '๊ฐ์1');3lec1.printIdType(); // id์ ํ์ ์ String์ ๋๋ค.45Lecture<int, String> lec2 = Lecture(123, '๊ฐ์2');6lec2.printIdType(); // id์ ํ์ ์ int์ ๋๋ค.7}89// generic : ํ์ ์ ์ธ๋ถ์์ ๋ฐ์ ๋ ์ฌ์ฉ10class Lecture<T, X> {11final T id;12final X name;1314Lecture(this.id, this.name);1516void printIdType() {17print('id์ ํ์ ์ ${T}์ ๋๋ค.');18}19}
6. OOP๋ผ๊ณ ๋ถ๋ฅด๋ ์ด์
1void main() {2Test t = Test();3}45class Test {}6// class Test extends Object {}7// ์ ์ฝ๋๊ฐ ์์ด๋, ๋ชจ๋ ํด๋์ค๊ฐ Object๋ฅผ ์์๋ฐ๋๋ค.
- ๋ชจ๋ ํด๋์ค์ ์ต์์ ๋ถ๋ชจ๋
Objectํด๋์ค์ด๋ค. - ์ฆ, ๋ชจ๋ ํด๋์ค๊ฐ Object๋ฅผ ์์๋ฐ๋๋ค.
- ๊ทธ๋ ๊ธฐ์, ๊ฐ์ฒด(Obejct) ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ด๋ผ ๋ถ๋ฅด๋ ๊ฒ์ด๋ค.