let studentA = {
name: "이정환",
grade: "A+",
age: 27,
study() {
console.log("열심히 공부 함");
},
introduce() {
console.log("안녕하세요!");
},
};
// 만약 이때 한명의 학생이 더 필요하다면 다음과 같이 새로운 변수를 만들어야 합니다.
let studentB = {
name: "홍길동",
grade: "A+",
age: 27,
study() {
console.log("열심히 공부 함");
},
introduce() {
console.log("안녕하세요!");
},
};
class Student {
// 필드
name;
age;
grade;
// 생성자 - 특수한 메서드로 실질적으로 객체를 생성하는 함수입니다.
constructor(name, grade, age) {
this.name = name;
this.grade = grade;
this.age = age;
// 이때 this는 객체이며 현재 만들고 있는 객체를 의미
}
}
// ⭐️ 클래스를 이용해서 만든 객체 -> 인스턴스
// 즉, studentB 는 스튜던트 인스턴스
const studentB = new Student("홍길동", "A+", 27);
// 이렇게 생성할 수 있음 인수 순서 맞춰서
class Student1 {
// [필드]
name;
grade;
age;
// [생성자]
constructor(name, grade, age) {
this.name = name;
this.grade = grade;
this.age = age;
}
// [메서드] - 아래처럼 메서드 추가 해주면 됨
study() {
console.log("열심히 공부 함");
}
introduce() {
console.log(`안녕하세요!`);
}
}
let studentBb = new Student1("홍길동", "A+", 27);
studentBb.study(); // console - 열심히 공부 함
studentBb.introduce(); // console - 안녕하세요!
class Student12 {
// [필드]
name;
age;
grade;
// [생성자]
constructor(name, grade, age) {
this.name = name;
this.grade = grade;
this.age = age;
}
// [메서드]
study() {
console.log("열심히 공부 함");
}
introduce() {
console.log(`안녕하세요 ${this.name} 입니다!`);
}
}
let studentBb1 = new Student12("홍길동", "A+", 27);
studentB.introduce(); // console - 안녕하세요 홍길동 입니다!
class StudentDeveloper extends Student {
// [필드]
name;
age;
grade;
favoriteSkill;
// [생성자]
constructor(name, grade, age, favoriteSkill) {
this.favoriteSkill = favoriteSkill;
}
// [메서드]
study() {
console.log("열심히 공부 함");
}
introduce() {
console.log(`안녕하세요!`);
}
programming() {
console.log(`${this.favoriteSkill}로 프로그래밍 함`);
}
}
class StudentDeveloper extends Student {
// [필드]
favoriteSkill;
// StudentDeveloper 에 추가시킬 필드만 입력
// [생성자]
constructor(name, grade, age, favoriteSkill) {
super(name, grade, age);
this.favoriteSkill = favoriteSkill;
}
// 메서드
programming() {
console.log(`${this.favoriteSkill}로 프로그래밍 함`);
}
}
constructor(name, grade, age, favoriteSkill) : StudentDeveloper 클래스에서 Student 클래스의 생성자를 함께 호출. 그렇지 않으면 생성되는 객체의 name, grade, age 값이 제대로 설정되지 않음super(name, grade, age); : super를 호출하고 인수로 name, grade, age를 전달하면 슈퍼 클래스(확장 대상 클래스)의 생성자를 호출class Employee {
// [필드]
name;
age;
position;
// [메서드]
work() {
console.log("일함");
}
}
tsconfig.json 파일에 noImpliciANY: false 로 하면 허용 됨class Employee {
// [필드]
name: string ;
age: number;
position: string;
// [메서드]
work() {
console.log("일함");
}
}
class Employee {
// [필드]
name: string = "";
age: number = 0;
position: string = "";
// [메서드]
work() {
console.log("일함");
}
}
class Employee1 {
// [필드]
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("일함");
}
}
const employeeB = new Employee1("박하늘", 25, "개발자")
class Employee1 {
(...)
}
const employeeC: Employee1 = {
name: "",
age: 0,
position: "",
work() {},
};
// Employee1 보다 확장
class ExecutiveOfficer extends Employee1 {
officeNumber: number;
constructor(
name: string,
age: number,
position: string,
officeNumber: number
) {
super(name, age, position);
this.officeNumber = officeNumber;
}
}
super(name, age, position); : js 에서는 super 없어도 가능하지만 TS 에서는 불가 에러 발생접근 제어자(Access Modifier)는 타입스크립트에서만 제공되는 기능으로 클래스의 특정 필드나 메서드를 접근할 수 있는 범위를 설정하는 기능
- public : 모든 범위에서 접근 가능
- private : 클래스 내부에서만 접근 가능
- proteced : 클래스 내부 또는 파생 클래스 내부에서만 접근 가능
기본값 - 객체이기도 하고 각각 필드에 자동으로 접근제어자인 public 이 걸려있음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("일함");
}
}
// 이런 클래스가 있을 경우 ...
const employee = new Employee('박하늘', 25, "developer")
employee.age = 30
employee.name = "바가늘"
// 이런식으로 클래스 외부에서도 프로퍼티에 접근하여 재선언(수정) 가능
class Employee1 {
private name: string ;
public age: number;
public position: string;
constructor(name: string, age: number, position: string) {
this.name = name;
this.age = age;
this.position = position;
}
work() {
console.log("일함");
}
}
const employee1 = new Employee1('박하늘', 25, "developer")
employee1.age = 30
employee1.name = "바가늘" // ❌ - private 을 걸어주면 클래스 외부에서 접근 불가
// 메서드에 (클래스 내부) 사용시에는 this 로 사용(접근) 가능
work() {
console.log(`${this.name}일함`);
}
class ExecutiveOfficer extends Employee1 {
officeNumber: number;
constructor(
name: string,
age: number,
position: string,
officeNumber: number
) {
super(name, age, position);
this.officeNumber = officeNumber;
}
func () {
this.name
}
}
Employee1 클래스를 상속받는 ExecutiveOfficer 클래스에서 Employee1 클래스에 선언된 필드인 name 도 사용 불가능class User {
protected age: number;
constructor(age: number) {
this.age = age;
}
}
class Developer extends User {
introduce() {
console.log(`저는 ${this.age}살 개발자입니다.`); // ✅ 가능 (자식 클래스)
}
}
const dev = new Developer(25);
// console.log(dev.age); // ❌ 외부 접근 불가
class Employee11 {
// 필드
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} 일함`);
}
}
class Employee11 {
// 생성자
constructor(
private name: string,
protected age: number,
public position: string
) {
this.name = name;
this.age = age;
this.position = position;
}
// 메서드
work() {
console.log(`${this.name} 일함`);
}
}
public 필드만 정의 가능interface CharacterInterface {
name: string;
moveSpeed: number;
move(): void;
}
// Character 는 CharacterInterface 인터페이스를 구현한다 라는 뜻
// CharacterInterface 를 필드로 사용하면 되는 느낌
class Character implements CharacterInterface {
constructor(
public name: string,
public moveSpeed: number,
private extra: string
// ⚠️ 이거처럼 클래스에 따로 정의 하면 private 사용 가능
) {}
move(): void {
console.log(`${this.moveSpeed} 속도로 이동!`);
}
}