
let studentA = {
name: "제노",
grade: "A+",
age: 27,
study() {
console.log("열심히 공부 함");
},
introduce() {
console.log("안녕하세요!");
},
};
let studentB = {
name: "비노",
grade: "B-",
age: 27,
study() {
console.log("열심히 공부 함");
},
introduce() {
console.log("안녕하세요!");
},
};
⇒ 즉, 객체를 찍어내는 틀
class Student {
// 필드: 클래스가 만들어 낼 객체 프로퍼티를 의미
// 어떤 모양의 객체를 찍어낼지 정의하는 것
name;
grade;
age;
// 생성자
// 클래스를 호출하면, 실제로 객체를 생성하는 역할을 하는 함수
constructor(name, grade, age) {
this.name = name;
this.grade = grade;
this.age = age;
// this는 클래스가 지금 만들고 있는 객체
}
// 메서드
// Student 클래스로 만들어지는 인스턴스는 study, introduce를 갖게 됌
study() {
console.log("열심히 공부 함");
}
introduce() {
console.log(`안녕하세요! ${this.name} 입니다!`);
}
}
let studentB = new Student("제노", "A+", 27);
⇒ 즉, 스튜던트 인스턴스라고 부를 수 있다.
studentB.study();
studentB.introduce();
class StudentDeveloper extends Student {
// 필드
favoriteSkill;
// 생성자
constructor(name, grade, age, favoriteSkill) {
super(name, grade, age);
// super로 name, grade, age 인수로 전달해줘야 한다.
// 메서드, 필드를 가져오지만, 생성자에서는 name, grade, age 프로퍼티 값이 설정이 되지 않기 때문에,
// super를 호출해서 넘겨주면 부모 클래스(Student)의 생성자가 호출된다.
this.favoriteSkill = favoriteSkill;
}
// 메서드
programming() {
console.log(`${this.favoriteSkill}로 프로그래밍 함`);
}
}
const studentDeveloper = new StudentDeveloper("제노", "B+", 27, "TypeScript");
console.log(studentDeveloper);
studentDeveloper.programming();
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 employeeB = new Employee("제노", 27, "개발자");
console.log(employeeB);
const employeeC: Employee = {
// 필드, 메서드를 갖고 있어야 함.
name: "",
age: 0,
position: "",
work() {},
};
⇒ access modifier
⇒ 종류로는 public, private, protected
class Employee {
// 필드
private name: string;
protected age: number;
public position: string;
// 생성자
constructor(
name: string,
age: number,
position: string
) {
this.name = name;
this.age = age;
this.position = position;
}
// 메서드
work() {
console.log(`${this.name} 일함`);
}
}
class Employee {
// 필드
// private name: string;
// protected age: number;
// public position: string;
// 생성자
constructor(
private name: string,
protected age: number,
public position: string
) {
// this.name = name;
// this.age = age;
// this.position = position;
//
}
// 메서드
work() {
console.log(`${this.name} 일함`);
// private: 사적인, 외부에서는 프로퍼티에 접근하는 것, 읽는 것 조차도 불가능 해진다. -> 내부에서는 가능
}
}
employee.name = "홍길동"; // 오류 발생
// private: 사적인, 외부에서는 프로퍼티에 접근하는 것, 읽는 것 조차도 불가능 해진다.
// -> 내부에서는 가능
// private으로 설정하면 오류가 발생
필드
생성자
⇒ 따라서, 필드 영역을 작성할 필요가 없다.
⇒ 작성하면 오히려 중복되었다고 오류가 발생
class ExecutiveOfficer extends Employee {
// 필드
officeNumber: number;
// 생성자
constructor(
name: string,
age: number,
position: string,
officeNumber: number
) {
super(name, age, position);
this.officeNumber = officeNumber;
}
func() {
this.name;
// 파생 클래스는 내부에서 접근이 불가능해진다.
this.age;
// protected: 외부에서 접근이 불가능 해지지만,
// 파생 클래스 내부에서 접근이 가능하도록 하고 싶을 때 사용
}
}
// 인터페이스와 클래스
interface CharacterInterface {
name: string;
moveSpeed: number;
move(): void;
}
// interface 타입의 객체를 생성할 수 있도록 정의할 수 있다.
class Character implements CharacterInterface {
constructor(public name: string, public moveSpeed: number) {}
// constructor(private name: string, protected moveSpeed: number) {}
// 이렇게 private, protected를 접근 제어자를 설정하면, 오류가 발생
// 인터페이스는 무조건 public 필드만 정의할 수 있기 때문
move() {
console.log(`${this.moveSpeed} 속도로 이동!`);
}
}