December 22, 2020
constructor
와 일반 메소드와는 다르게 속성은 val : type
과 같이 클래스 바디에 별도로 타입을 선언한다. class Animal {
name : string
constructor(name : string){
this.name = name;
}
}
class Dog extends Animal {
getName() : string {
return `강아지 이름은 ${this.name} 입니다.`
}
}
const dob : Dog = new Dog('백돌시');
public
: 어디서나 자유롭게 접근가능(기본) - 속성, 메소드protected
: 나와 상속된 클래스 내에서 접근 가능 - 속성, 메소드private
: 현재 클래스에서만 접근 가능 - 속성, 메소드static
: 정적으로 사용 - 속성, 일반 메소드readonly
: 읽기 전용으로 사용 - 속성 class Animal {
constructor(public name : string){
}
}
class Dog extends Animal {
getName() : string {
return `강아지 이름은 ${this.name} 입니다.`
}
}
const dog : Dog = new Dog('백돌시');
static
을 사용하여 정적 변수나, 정적 메소드를 생성할 수 있다.static
이란, 클래스 자체가 가지고있는 속성이나 메소드이다. class Animal {
static type : string = '동물'
constructor(public name : string){
this.name = name;
}
}
class Dog extends Animal {
getName() : string {
return `강아지 이름은 ${this.name} 입니다. ${Animal.type} 입니다.`
}
}
const dog : Dog = new Dog('백돌시');
console.log(dog.getName())
abstract class AUser {
abstract name : string
abstract getName() : string
}
class User1 extends AUser {
constructor(public name : string){
super()
}
getName(){
return this.name
}
}
interface IUser {
name : string
getName() : string
}
class User2 implements IUser {
constructor(public name : string){}
getName(){
return this.name
}
}
const user1 = new User1('상민');
const user2 = new User2('상민');
abstract class AUser {
abstract name : string
abstract getName() : string
constructor(public age : number){}
getAge(){
return this.age
}
}
class User1 extends AUser {
constructor(public name : string, age : number){
super(age)
}
getName(){
return this.name
}
}
const user1 = new User1('상민', 26);
console.log(user1.getName(), user1.getAge())