Classes (ν΄λμ€)
κ°μ²΄λ₯Ό μμ±νκΈ° μν νλμ ν νλ¦Ώ μν λ‘, ES6μμ λμ λ ν΄λμ€ λ¬Έλ²μ κΈ°λ°μΌλ‘ νλ€.
μμ±μ, μμ±, λ©μλλ‘ κ΅¬μ±λμ΄μλ€.
// ν΄λμ€ μ μ
class Continent {
continentName: string;
constructor(name: string) {
this.continentName = name;
}
getContinentName() {
return this.continentName;
}
}
class Country extends Continent {
name: string;
capital: string;
constructor(continentName: string, name: string, capital: string) {
super(continentName) // λΆλͺ¨λ‘λΆν° μμ
this.name = name;
this.capital = capital;
}
getInfo() {
// μμλ°μ ν΄λμ€μ μμμ μ κ·Όνλ €λ©΄ getContinentName λ©μλλ₯Ό ν΅ν΄ μ κ·Όνλ€
return `${this.name}, ${this.capital}, ${this.getContinentName()}`;
}
}
// μΈμ€ν΄μ€ μμ±
let country = new Country(
'Asia', 'South Korea', 'Seoul'
);
console.log(country.getInfo()) // "South Korea, Seoul, Asia"
μ κ·Ό μ μ΄μ(Access Modifiers)
Typescriptμλ ν΄λμ€μ μμ±κ³Ό λ©μλμ μ κ·Ό λ²μλ₯Ό μ μ΄ν μ μλ μ κ·Ό μ μ΄μκ° μλ€.
- μ’ λ₯
- public : κΈ°λ³Έκ°, μ΄λμλ μ κ·Ό κ°λ₯
- protected : ν΄λΉ ν΄λμ€ λ΄λΆ λ° μλΈν΄λμ€μμ μ κ·Ό κ°λ₯
- private / # (ν΄μ μ΄λ¦) : ν΄λΉ ν΄λμ€ λ΄λΆμμλ§ μ κ·Ό κ°λ₯
class Continent {
// μ κ·Ό μ μ΄μ
private continentName: string;
constructor(name: string) {
this.continentName = name;
}
getContinentName() {
return this.continentName;
}
}
class Country extends Continent {
name: string;
capital: string;
constructor(continentName: string, name: string, capital: string) {
super(continentName) // λΆλͺ¨λ‘λΆν° μμ
this.name = name;
this.capital = capital;
}
getInfo() {
// privateμ κ²½μ°μλ§ μλ¬,
// protected, publicμμλ μ κ·Όμ΄ κ°λ₯
this.continentName;
return `${this.name}, ${this.capital}, ${this.getContinentName()}`;
}
}
ES6λΆν°λ ν΄μλ₯Ό μ¬μ©ν μ μλ€.
class Country extends Continent {
// ν΄μ μ¬μ©
#name: string;
capital: string;
constructor(continentName: string, name: string, capital: string) {
super(continentName) // λΆλͺ¨λ‘λΆν° μμ
this.#name = name;
this.capital = capital;
}
getInfo() {
this.continentName;
return `${this.#name}, ${this.capital}, ${this.getContinentName()}`;
}
}
// μΈμ€ν΄μ€ μμ±
let country = new Country(
'Asia', 'South Korea', 'Seoul'
);
country.name; // μ κ·Ό λΆκ°λ₯!
μΈν°νμ΄μ€λ‘ ν΄λμ€ κ΅¬μ‘° μ μ
// μΈν°νμ΄μ€ μ μ
interface ContinentInterface {
getContinentName(): string
}
interface CountryInterface {
// μ§κΈμ nameμ΄ private(#)μΈ μνμ΄λ―λ‘ μ μΈ
capital: string;
getInfo(): string;
}
// ν΄λμ€ μ μ
class Continent implements ContinentInterface {
protected continentName: string;
constructor(name: string) {
this.continentName = name;
}
getContinentName() {
return this.continentName;
}
}
// extends λ¨Όμ μ¬μ©, implementsλ κ·Έ λ€μ
class Country extends Continent implements CountryInterface {
#name: string;
capital: string;
constructor(continentName: string, name: string, capital: string) {
super(continentName) // λΆλͺ¨λ‘λΆν° μμ
this.#name = name;
this.capital = capital;
}
getInfo() {
this.continentName;
return `${this.#name}, ${this.capital}, ${this.getContinentName()}`;
}
}
// μΈμ€ν΄μ€ μμ±
let country = new Country(
'Asia', 'South Korea', 'Seoul'
);
console.log(country.getInfo())
μΆμν΄λμ€
abstract class AbstractCountry {
name: string;
capital: string;
constructor(name: string, capital: string) {
this.name = name;
this.capital = capital;
}
setup(): void {
console.log("setup complete")
}
abstract displayInfo(): void;
}
// μΆμ ν΄λμ€λ μΈμ€ν΄μ€ μ¬μ©μ΄ λΆκ°λ₯
// μμλ°λ μ ν΄λμ€λ₯Ό μμ±ν΄μ€μΌλ¨
class MyCountry extends AbstractCountry {
displayInfo(){
console.log("display info called")
}
}
const abstractCountry = new MyCountry("Germany", "Berilin")
abstractCountry.setup();
abstractCountry.displayInfo();
'typescript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
TypeScript / νμ μ μΈ νμΌ (4) | 2025.06.10 |
---|---|
TypeScript / μ λ€λ¦ (2) | 2025.06.05 |
TypeScript / νμ μΆλ‘ , νμ λ¨μΈ (0) | 2025.06.05 |
TypeScript / νμ (2) (0) | 2025.06.04 |
TypeScript / νμ (1) (0) | 2025.06.04 |