본문 바로가기
IT/TypeScript

접근 제한자(access modifier) 와 생성자(constructor), 클래스,인터페이스

by 골든크랩 2022. 10. 20.
728x90
반응형

 

■ 접근 제한자 종류 : public, private, protect

   지정하지 않으면 모두 public

 

■ 생성자

constructor 라는 예약어를 사용함.

 

특이점은 생성자에서 멤버 변수를  선언할 수 있다.

 

예)

class School {

     constructor(public schoolName: string, public schoolAddress?: string) {}

}

let myschool : School = new School('멋진고등학교', '대전시 서구 용문동')

console.log(myschool)

 

인터페이스를 구현한 클래스 예

 

interface ISchool {

    schoolName : string

    schoolAddress : string

}

 

class School implements ISchool {

    constructor(public schoolName: string, public schoolAddress: string) {}

}

let mySchool : School = new School('멋진고', '대전시 서구 용문동')

console.log(mySchool)

 

■ 추상클래스

예)

abstract class AbstractSchool {

    abstract schoolName : string

    constructor(public schoolAddress : number) {}

}

class School extends AbstractSchool {

    constructor(public schoolName: string, schoolAddress: string) {

        super(schoolAddress)

    }

}

 

let mySchool : School = new School('멋진고', '대전시 서구 용문동')

console.log(mySchool)

 

■ 정적 메서드

예)

class Interest {

    static interestRate = 3.5

}

 

let shinhanBankIR = Interest.interestRate

 

 

 

 

 

 

 

 

 

 

728x90
반응형

댓글