// TypeScript์์์ ํด๋์ค ์ด๋
ธํ
์ด์
์ฒ๋ฆฌ
class Player{
first: string;
last: string;
score: number = 0;
constructor(first: string, last: string){
this.first = first;
this.last = last;
}
}
const elton = new Player("Elton", "Steele");
class Player{
readonly first: string;
readonly last: string;
score: number = 0;
constructor(first: string, last: string){
this.first = first;
this.last = last;
}
}
const elton = new Player("Elton", "Steele");
elton.first = "elton"; //error
๊ธฐ๋ณธ์ ์ผ๋ก public์ด๋ฏ๋ก ๋ฐ๋ผ์ public ์ ๊ทผ ์ ์ด์๋ ํ์๋ ์๋์ง๋ง, ๋ค๋ฅธ ๊ฐ๋ฐ์๊ฐ ํด๋์ค ์ธ๋ถ์์๋ ๋ณ๊ฒฝ, ์ ๊ทผ,์ฐ๊ธฐ๋ฅผ ํ ์ ์๋ค๊ณ ๋ช ํํ ์๋ฆฌ๊ธฐ์ํด public์ ์ฌ์ฉํ๋ฉฐ ์ด๋์์๋ ์ธ ์ ์๋ค.
class Player{
public readonly first: string;
public readonly last: string;
public score: number = 0;
constructor(first: string, last: string){
this.first = first;
this.last = last;
}
}
const elton = new Player("Elton", "Steele");
private์ ํด๋น ํ๋กํผํฐ ๋๋ ๋ฉ์๋๊ฐ ์ค์ง ํด๋์ค์ ๋ด๋ถ์์๋ง ์ ๊ทผ ๋ฐ ์ฌ์ฉํ ์ ์๋ค.
private์ ํ๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค.
ํ๋กํผํฐ ๋๋ ๋ฉ์๋ ์์
1. private์ ์ถ๊ฐ
2. #์ถ๊ฐ
class Player{
public readonly first: string;
public readonly last: string;
private score: number = 0; // 1. private
#score: number = 0; // 2. private
constructor(first: string, last: string){
this.first = first;
this.last = last;
}
}
const elton = new Player("Elton", "Steele");
elton.score; // error 1. private
elton.#score; // error 2. private
class Player{
// public readonly first: string;
// public readonly last: string;
// private score: number = 0;
constructor(
public first: string,
public last: string,
private score: number){
}
}
const elton = new Player("Elton", "Steele", 100);
class Player{
public readonly first: string;
public readonly last: string;
private _score: number = 0;
constructor(
public first: string,
public last: string,
private _score: number){}
get score(): number{
return this._score;
}
set score(newScore: number){
if(newScore < 0){
throw new Error("Can not");
}
return this._score;
}
}
const elton = new Player("Elton", "Steele", 100);
elton.score = 99;
console.log(elton.score); // 99
์์ ์์
๋ ์ฌ์ฉ๋๋ฉฐ protected๋ฅผ ์ฌ์ฉํ๋ฉด ์ธ๋ถ์์ ์ก์ธ์คํ๋ ๊ฒ ๋ถ๊ฐ๋ฅํ์ง๋ง
์์ ํด๋์ค๋ ์ก์ธ์ค๊ฐ ๊ฐ๋ฅํ๋ค.
class Player{
constructor(
public first: string,
public last: string,
protected _score: number){
}
}
class SuperPlayer extends Player{
public isAdmin:boolean = true;
maxScore(){
this._score = 999;
}
}
public์ ์ด๋์๋ ์ก์ธ์ค ๊ฐ๋ฅํ๊ณ
private์ ์ ์๋ ํด๋น ํด๋์ค์์๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๊ณ
protected๋ ์ ์๋ ํด๋์ค์ ๊ทธ๋ก๋ถํฐ ์์ํ ๋ชจ๋ ํด๋์ค์์ ์ก์ธ์คํ ์ ์๋ค.
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ํด๋์ค๋ ๊ฐ๋จํ์ง๋ง ์์ฃผ ํํ ์ฌ์ฉ๋๋ ๋ฐฉ๋ฒ์ด๋ค.
interface Colorful{
color: string;
}
interface Printable{
print(): void;
}
class Bike implements Colorful{
constructor(public color: string){}
}
class Jacket implements Colorful, Printable{
constructor(public brand:string, public color:string){}
print(){
console.log(` ${this.color} ${this.brand} jacket`);
}
}
TypeScript์์์ Abstract์ ์ด ์์ฒด๋ก๋ ๋ ์ด์ ์ ํด๋์ค๋ฅผ ๋ง๋ค ์ ์๋ค๋๊ฑธ ์๋ฏธํ๋ค.
abstract ํด๋์ค๋ ํจํด์ ์ ์ํ๊ณ ์์ ํด๋์ค์์ ์ํ๋ผ์ผ ํ๋ ๋ฉ์๋๋ฅผ ์ ์ํ๋๋ฐ ์ฌ์ฉ๋๋ค.
์ธํฐํ์ด์ค๋ ๊ฐ์ฒด์ ํํ๋ง ์กด์ฌํ๊ณ abstract ํด๋์ค๋ ํ์ฅํ๋ ํด๋์ค์ ๊ธฐ๋ฅ ์ฆ, ํจํด์ ์ ์ํ ์ ์๋ค.
๋ฉ์๋ ์์ abstract๋ฅผ ๋ถ์ด๋ฉด ์์ ํด๋์ค๊ฐ ํด๋น ๋ฉ์๋๋ฅผ ๋ฐ๋์ ์ํํด์ผํ๋ค.
abstract class Employee{
constructor(public first: string, public last: string){}
abstract getPay(): number; //Employee๋ฅผ ํ์ฅํ๋ ๋ชจ๋ ํด๋์ค์ ์กด์ฌํด์ผํ๋ค
greet(){
console.log("hi");
}
}
class FullTimeEmployee extends Employee{
constructor(first: string, last: string, private salary: number){
super(first,last);
}
getPay(): number {
return this.salary;
}
}
class PartTimeEmployee extends Employee{
constructor(
first: string,
last: string,
private hourlyRate: number,
private hoursWorked: number){
super(first,last);
}
getPay(): number {
return this.hourlyRate * this.hoursWorked;
}
}
const betty = new FullTimeEmployee("Betty", "White", 9500);
console.log(betty.getPay()); // 9500
console.log(betty.greet()); // hi
ํ์ฅํ abstract ํด๋์ค์ interface๋ ๋์์ ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค