oop(=객체 지향 프로그래밍)
클래스 : 연관된 변수와 함수들을 한 덩어리로 묶음
클래스와 객체
- 클래스는 객체의 뼈대 / 설계도, 생산틀 / 붕어빵틀
- 객체는 클래스의 실체 / 불어빨
실습으로 만들어보기
//멤버변수 = 속성 = 프로퍼티
//멤버함수 = 메소드
//클래스
class Employee {
//멤버 변수
empName: string;
age: number;
empjob: string;
printEmp = (): void => {
console.log(
`${this.empName}의 나이는 ${this.age}이고, 직업은 ${this.empjob}입니다.`
);
};
}
//객체
let employee1 = new Employee();
employee1.empName = "kim";
employee1.age = 20;
employee1.empjob = "개발자";
employee1.printEmp();
생성자
위 코드 예제에서 멤버 변수에 값을 할당하는 부분이 불편함
//멤버변수 = 속성 = 프로퍼티
//멤버함수 = 메소드
//클래스
class Employee {
//멤버 변수
empName: string;
age: number;
empjob: string;
//생성자 -> 객체 생성시 자동 생성
//다른 언어에서 생성자는 클래스 이름과 같음
//this = 객체 자기자신
constructor(empName: string, age: number, empjob: string) {
this.empName = empName;
this.age = age;
this.empjob = empjob;
}
printEmp = (): void => {
console.log(
`${this.empName}의 나이는 ${this.age}이고, 직업은 ${this.empjob}입니다.`
);
};
}
//객체
//다른 언어의 생성자는 클래스 이름과 같다는 걸 기억
//그럼 아래 코드의 Employee는 생성자라고 생각해도 무관
let employee1 = new Employee("kim", 20, "개발자");
employee1.printEmp();
위 코드의 멤버변수는 외부에서 접근 가능함
즉
let employee1 = new Employee("kim", 20, "개발자");
employee1.empName = "lee";
employee1.printEmp();
같이 데이터가 외부에 노출돼 있음
=> 보호할 필요성 O
접근 지정자
: private, public, protected
- private 변수 앞에는 _언더바 붙이기
class Employee {
//멤버 변수 -> private 시켜줌
private _empName: string;
private _age: number;
private _empjob: string;
constructor(empName: string, age: number, empjob: string) {
this._empName = empName;
this._age = age;
this._empjob = empjob;
}
printEmp = (): void => {
console.log(
`${this._empName}의 나이는 ${this._age}이고, 직업은 ${this._empjob}입니다.`
);
};
}
let employee1 = new Employee("kim", 20, "개발자");
employee1.empName = "lee"; // 오류
employee1.printEmp();
getter외 setter
: 멤머변수를 private 하게 만들면 외부에서 아예 사용 불가
: 그럼 또 만든 이유가 없어짐, 이용 불가
class Employee {
private _empName: string;
private _age: number;
private _empjob: string;
constructor(empName: string, age: number, empjob: string) {
this._empName = empName;
this._age = age;
this._empjob = empjob;
}
//get/set => 쌍으로 만들어주는게 관례
get empName() {
return this._empName;
}
set empName(val: string) {
this._empName = val;
}
printEmp = (): void => {
console.log(
`${this._empName}의 나이는 ${this._age}이고, 직업은 ${this._empjob}입니다.`
);
};
}
//객체
let employee1 = new Employee("kim", 20, "개발자");
employee1.empName = "lee"; // 클래스 안 set empName 호출
employee1.printEmp();
위 코드의 멤버변수를 보면 중복이 너무 많음
따라서
class Employee {
//생성자의 파라미터는 암묵적으로 클래스의 맴버변수로 선언됨
//동시에 전달 인자로도 사용됨
constructor(
private _empName: string,
private _age: number,
private _empjob: string
) {}
get empName() {
return this._empName;
}
set empName(val: string) {
this._empName = val;
}
printEmp = (): void => {
console.log(
`${this._empName}의 나이는 ${this._age}이고, 직업은 ${this._empjob}입니다.`
);
};
}
//객체
let employee1 = new Employee("kim", 20, "개발자");
employee1.empName = "lee"; // 클래스 안 set empName 호출
employee1.printEmp();
위와 같이 constructor의 ()에 선언해주면 자동 선언 및 할당됨
'데브코스 > 강의 정리' 카테고리의 다른 글
JSX 문법 - 최상위태그필수, 인라인스타일, 주석 (1) | 2024.10.30 |
---|---|
리액트란 / 초기설정 / 기본 코드 분석 (0) | 2024.10.30 |
리터럴, any, 유니온, array, tuple (0) | 2024.10.29 |
오버로딩, 오버라이딩, 인터페이스, 람다 (2) | 2024.10.28 |
객체 지향, 추상화, 캡슐화, 클래스, 생성자, 상속성 (0) | 2024.10.28 |