๐ฎ ์ง๋
6. ํด๋์ค (4/4)
- ์๋ฐ์คํฌ๋ฆฝํธ์ ํด๋์ค ์๊ฐ
- ํ์ ์คํฌ๋ฆฝํธ์ ํด๋์ค
- ์ ๊ทผ์ ์ด์
- ์ธํฐํ์ด์ค์ ํด๋์ค
๐ฅ ์์ฝ
์๋ฐ์คํฌ๋ฆฝํธ์ ํด๋์ค
/**
* ํด๋์ค class
*/
let studentA = {
name: "Julie",
grade: "A+",
age: 24,
study() {
console.log("์ด์ฌํ ๊ณต๋ถํจ");
},
introduce() {
console.log("์๋
ํ์ธ์");
},
};
class Student {
// field
name;
grade;
age;
// constructor
constructor(name, grade, age) {
this.name = name;
this.grade = grade;
this.age = age;
}
// method
study() {
console.log("์ด์ฌํ ๊ณต๋ถํจ");
}
introduce() {
console.log("์๋
ํ์ธ์");
}
}
class StudentDeveloper extends Student {
// field
favoriteSkill;
// constructor
constructor(name, grade, age, favoriteSkill) {
super(name, grade, age);
// Student์ field๊ฐ ๊ทธ๋๋ก ๊ฐ์ ธ์ค๊ฒ ๋๋๋ฐ ์ฌ๊ธฐ์๋ ๋ฐ์ favoriteSkill๋ง ์์ฑ์ ํจ์ ์์ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ์ super()๋ก ์ธ์๋ฅผ ์ ๋ฌํด์ผ ์ธ์คํด์ค๊ฐ ์์ฑ๋จ
this.favoriteSkill = favoriteSkill;
}
// method
programming() {
console.log(`${this.favoriteSkill}๋ก ๊ฐ๋ฐํจ`);
}
}
let studentDeveloper = new StudentDeveloper(
"ํ
์คํธ๋ค์",
"ํ
์คํธ๊ทธ๋ ์ด๋",
"ํ
์คํธ์์ด์ง",
"ํ
์คํธ์คํฌ"
);
console.log(studentDeveloper);
studentDeveloper.programming();
// ํด๋์ค๋ฅผ ์ด์ฉํด์ ๋ง๋ ๊ฐ์ฒด => ์ธ์คํด์ค
// ์ด๊ฑฐ๋ student๋ผ๋ ํด๋์ค๋ฅผ ์ด์ฉํด์ ๋ง๋ ์ธ์คํด์ค๋ผ์ student instance ๋ผ๊ณ ๋ถ๋ฅด๋ฉด ๋๋ค
let studentB = new Student("Bell", "B+", 26);
console.log(studentB);
ํ์ ์คํฌ๋ฆฝํธ์ ํด๋์ค
/**
* ํ์
์คํฌ๋ฆฝํธ์ ํด๋์ค
*/
const employee = {
name: "Julie",
age: 24,
position: "developer",
work() {
console.log("dobby");
},
};
// class
class Employee {
// field
name: string;
age: number;
position: string;
// constructor
constructor(name: string, age: number, position: string) {
this.name = name;
this.age = age;
this.position = position;
}
// method
work() {
console.log("do hard work");
}
}
const employeeB = new Employee("Hanul", 24, "developer");
console.log(employeeB);
const employeeC: Employee = {
name: "",
age: 0,
position: "",
work() {},
};
// ๋ ์ธ๋ถํ๋ ํด๋์ค
class ExecutiveOfficer extends Employee {
// field
officeNumber: number;
// constructor
constructor(
name: string,
age: number,
position: string,
officeNumber: number
) {
super(name, age, position);
this.officeNumber = officeNumber;
}
}
์ ๊ทผ์ ์ด์
/**
* ์ ๊ทผ์ ์ด์
*/
// class
class Employee {
// field
// private name: string;
// protected age: number;
// public position: string;
// constructor
// ๋งค๊ฐ๋ณ์ ์์ ๋ฌ์์ฃผ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ฐ ์์ฑ์์ ์ ๊ทผ์๋ฅผ ๋ฌ๋ฉด ํ๋๋ ์๋์ผ๋ก ์์ฑํด์ฃผ๊ณ (์๋ตํ์), ํ๋๊ฐ ์ด๊ธฐํ๋ ์๋์ผ๋ก ํด์ฃผ๊ธฐ ๋๋ฌธ์ ์๋ต(์๋ต์ ํ)ํด์ผํจ
constructor(private name: string, protected age: number,public position: string) {
this.name = name;
this.age = age;
this.position = position;
}
// method
work() {
console.log("do hard work");
}
}
class ExecutiveOfficer extends Employee {
// field
officeNumber: number;
// constructor
constructor(
name: string,
age: number,
position: string,
officeNumber: number
) {
super(name, age, position);
this.officeNumber = officeNumber;
}
// method
func() {
// this.name;
// ํ์ ํด๋์ค์์๋ ์์ํด๋์ค์์ private์ผ๋ก ์ค์ ํ field์ ๊ฐ์ ์ ๊ทผํ ์ ์๋ค. Employee ํด๋์ค ๋ด์ ์๋ง ์ ๊ทผ ๊ฐ๋ฅ
this.age; // ์ธ๋ถ์์ ๋ถ๊ฐํ์ง๋ง ํ์ํด๋์ค์๋ ์ ๊ทผ ๊ฐ๋ฅํ๋ ค๋ฉด protected๋ก
}
}
const employee = new Employee('์์ง์', 24, 'developer');
// employee.name='๊ฐ๋๋ค'; // private ์์ฑ์ด๋ฉด ํด๋น ํด๋์ค ๋ด์์๋ง ์ ๊ทผ๊ฐ๋ฅ
// employee.age=29; // protected ์์ฑ์ด๋ฉด ์ธ๋ถ์์ ์ ๊ทผ๋ถ๊ฐํ์ง๋ง ํ์ํด๋์ค์์๊น์ง๋ ์ ๊ทผ์ด ๊ฐ๋ฅ
employee.position='designer'; // public
console.log(employee);
์ธํฐํ์ด์ค์ ํด๋์ค
/**
* ์ธํฐํ์ด์ค์ ํด๋์ค
*/
interface CharacterInterface {
name: string;
moveSpeed: number;
move(): void;
}
class Character implements CharacterInterface {
// name: string;
// moveSpeed: number;
// ์ธํฐํ์ด์ค๋ก ์ ์ํ๋ ํ๋๋ ๋ฌด์กฐ๊ฑด public
// protected๋ private์ ์ ์ํ๋ ค๋ฉด ๋ฐ์ constructor์ ๋ฐ๋ก ์ ์ํด์ผํจ
constructor(
public name: string,
public moveSpeed: number,
private extra: string
) // ์ด๊ฒ์ฒ๋ผ constructor์ ์ ์
{
// this.name = name;
// this.moveSpeed = moveSpeed;
}
move(): void {
console.log(`${this.moveSpeed} ์๋๋ก ์ด๋!`);
}
}
๐ต ๊ณผ์
Q. ๊ตฌํ ์๊ทธ๋์ฒ๋ฅผ ์์ฑํ์ธ์.
class Pokemon {
constructor(
public name: string,
public skill: string,
readonly type: string
) {}
getName() {
return this.name;
}
setSkill(skill :string): void {
this.skill = skill;
}
}'lecture recap' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| CS50 : C์ธ์ด (0) | 2024.10.27 |
|---|---|
| TS: ํ์ ํฌ๊ธฐ๋ก ์๋ผ๋จน๋ ํ์ ์คํฌ๋ฆฝํธ ์ฑ๋ฆฐ์ง_day10 (0) | 2024.08.09 |
| TS: ํ์ ํฌ๊ธฐ๋ก ์๋ผ๋จน๋ ํ์ ์คํฌ๋ฆฝํธ ์ฑ๋ฆฐ์ง_day8 (0) | 2024.08.09 |
| TS: ํ์ ํฌ๊ธฐ๋ก ์๋ผ๋จน๋ ํ์ ์คํฌ๋ฆฝํธ ์ฑ๋ฆฐ์ง_day7 (0) | 2024.08.09 |
| TS: ํ์ ํฌ๊ธฐ๋ก ์๋ผ๋จน๋ ํ์ ์คํฌ๋ฆฝํธ ์ฑ๋ฆฐ์ง_day6 (0) | 2024.08.09 |
