ν μ ν¬κΈ°λ‘ μλΌλ¨Ήλ νμ μ€ν¬λ¦½νΈ(TypeScript) κ°μ | μ΄μ ν Winterlood - μΈνλ°
μ΄μ ν Winterlood | , νλ‘ νΈμλμ νΌν μ μλ λμΈ νμ μ€ν¬λ¦½νΈ,μ΄μ λ μ λλ‘ μ 볡ν λκ° μμ΅λλ€! π [μ¬μ§]μΈνμ½ 2023 'νμ μ€ν¬λ¦½νΈλ μ κ·Έλ΄κΉ?' λ°νμμ κ°μμ λλ€. π§ λ°°μ
www.inflearn.com
π μλ°μ€ν¬λ¦½νΈμ ν΄λμ€ μκ°
// β
λΆλͺ¨ ν΄λμ€
class Student {
// νλ
name;
grade;
age;
// μμ±μ
constructor(name, grade, age) {
this.name = name;
this.grade = grade;
this.age = age;
}
// λ©μλ
study() {
console.log("μ΄μ¬ν κ³΅λΆ ν¨");
}
introduce() {
console.log(`μλ
νμΈμ! μ λ ${this.name} μ
λλ€!`);
}
};
// β
μμ ν΄λμ€ (Student ν΄λμ€λ₯Ό νμ₯)
class StudentDeveloper extends Student {
// μΆκ° νλ
favoriteSkill;
// μμ±μ
constructor(name, grade, age, favoriteSkill) {
super(name, grade, age); // λΆλͺ¨ μμ±μ νΈμΆ
this.favoriteSkill = favoriteSkill;
}
// μΆκ° λ©μλ
programming() {
console.log(`${this.favoriteSkill}λ‘ νλ‘κ·Έλλ° ν¨`)
}
}
let studentA = new Student("κ°μ°μ", "A+", 24);
let studentDeveloper = new Student("κ°μ°μ", "A+", 24, "TypeScript");
π νμ μ€ν¬λ¦½νΈμ ν΄λμ€
// β
νμ
μ€ν¬λ¦½νΈμ ν΄λμ€
class Employee {
// νλ
name: string;
age: number;
position: string;
// μμ±μ
constructor(name: string, age: number, position: string) {
this.name = name;
this.age = age;
this.position = position;
};
// λ©μλ
work() {
console.log("μ΄μ¬ν μΌ ν¨");
};
};
class ExecutiveOfficer extends Employee {
// νλ
officeNumber: number;
// μμ±μ
constructor(name: string, age: number, position: string, officeNumber: number) {
super(name, age, position);
this.officeNumber = officeNumber;
};
};
// ν΄λμ€ μΈμ€ν΄μ€ μμ±
const employeeA = new Employee("κ°μ°μ", 24, "QA");
// ν΄λμ€λ νμ
μΌλ‘λ μ¬μ© κ°λ₯
const employeeB: Employee = {
name: "κ°λ§λ¦¬",
age: 5,
position: "κ·μλ―Έ",
work() {}
};
π μ κ·Ό μ μ΄μ
// β
μ κ·Ό μ μ΄μ (public / private / protected)
class Employee {
// μμ±μμμ μ κ·Ό μ μ΄μ μ€μ + νλ μ μΈ μλ΅
constructor(
private name: string, // ν΄λμ€ λ΄λΆμμλ§ μ κ·Ό κ°λ₯
protected age: number, // ν΄λμ€ + μμλ°μ ν΄λμ€ λ΄λΆμμ μ κ·Ό κ°λ₯
public position: string // μ΄λμλ μ κ·Ό κ°λ₯
) {}
// λ©μλ
public work() {
console.log(`${this.name} μΌν¨`); // ν΄λμ€ λ΄λΆμ΄λ―λ‘ private νλ μ κ·Ό κ°λ₯
}
}
const employee = new Employee("κ°μ°μ", 24, "QA");
// μΈλΆ μ κ·Ό ν
μ€νΈ
// employee.name = "νκΈΈλ"; // private νλμ΄λ―λ‘ μ κ·Ό λΆκ°
// employee.age = 25; // protected νλμ΄λ―λ‘ μ κ·Ό λΆκ°
employee.position = "QA"; // public νλμ΄λ―λ‘ μ κ·Ό κ°λ₯
// β
μμ ν
μ€νΈ
class ExecutiveOfficer extends Employee {
// νλ
officeNumber: number;
// μμ±μ
constructor(name: string, age: number, position: string, officeNumber: number) {
super(name, age, position);
this.officeNumber = officeNumber;
};
printInfo() {
// this.name; // private νλ μ κ·Ό λΆκ°
console.log(`λμ΄: ${this.age}`); // protected μ κ·Ό κ°λ₯
console.log(`μ§μ±
: ${this.position}`); // public μ κ·Ό κ°λ₯
}
};
π μΈν°νμ΄μ€μ ν΄λμ€
// β
μΈν°νμ΄μ€μ ν΄λμ€
// μΈν°νμ΄μ€λ ν΄λμ€μ "μ€κ³λ" μν
// → ν΄λμ€κ° μ΄λ€ νλ‘νΌν°μ λ©μλλ₯Ό λ°λμ κ°μ ΈμΌ νλμ§ μ μ
// → μΈν°νμ΄μ€ λ΄μ νλλ λͺ¨λ publicμ΄ κΈ°λ³Έ (λ€λ₯Έ μ κ·Ό μ μ΄μλ μ¬μ© λΆκ°λ₯)
interface CharacterInterface {
name: string;
moveSpeed: number;
move(): void;
}
class Character implements CharacterInterface {
constructor(public name: string, public moveSpeed: number) {};
move(): void {
console.log(`${this.moveSpeed} μλλ‘ μ΄λ!`)
};
};
β¬οΈ λ€μ λ΄μ©μ μλ κΈμ μ°Έκ³
μΈνλ° κ°μ - ν μ ν¬κΈ°λ‘ μλΌλ¨Ήλ νμ μ€ν¬λ¦½νΈ (μ λ€λ¦)
ν μ ν¬κΈ°λ‘ μλΌλ¨Ήλ νμ μ€ν¬λ¦½νΈ(TypeScript) κ°μ | μ΄μ ν Winterlood - μΈνλ°μ΄μ ν Winterlood | , νλ‘ νΈμλμ νΌν μ μλ λμΈ νμ μ€ν¬λ¦½νΈ,μ΄μ λ μ λλ‘ μ 볡ν λκ° μμ΅λλ€! π [μ¬μ§]
isliife2.tistory.com