TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day9

2024. 8. 9. 01:01ยทlecture recap

๐Ÿฎ ์ง„๋„

 

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
'lecture recap' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • CS50 : C์–ธ์–ด
  • TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day10
  • TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day8
  • TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day7
nuew
nuew
๐Ÿคธ ์žฌ์ฃผ ๋„˜๋Š” ์ค‘
  • nuew
    bloggg. . .๐Ÿฆ–๐Ÿ’ฅ
    nuew
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (88)
      • issue (10)
      • baekjoon (41)
      • lecture recap (11)
      • What I Learn (26)
      • retrospective (0)
      • maeil-mail (0)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    TailwindCSS
    Study
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
    Algorithm
    modal
    ๋ฐฑ์ค€
    issue
    what i learn
    TypeScript
    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
    JavaScript
    Node.js
    ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    media-query
    zustand
    css
    ํ•œ์ž…ํฌ๊ธฐ๋กœ์ž˜๋ผ๋จน๋Š”ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    Baekjoon
    js
    ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
nuew
TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day9
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”