캡슐화

TypeScript

protected, static 키워드

protected 키워드 class User{ protected x=10; } protected 키워드를 통해 필드 생성 class NewUser extends User{ doThis(){ this.x = 20; } } => extends로 상속받은 자식까지 필드값을 사용할 수 있게 제한. this 연산자를 통해 부모의 필드값에 접근할 수 있음. static 키워드 class User { x = 10; y = 20; } let john = new User(); john.x //가능 User.x //불가능 static 사용 전 => 인스턴스 객체로 필드값을 접근할 수 있으나, 클래스. 필드 방법으로는 접근할 수 없음. class User { static x = 10; y = 20; } let john = ..

TypeScript

public, private 키워드

public 키워드 class User { public name: string; constructor(){ this.name = 'kim'; } } let 유저1 = new User(); 유저1.name = 'park'; //가능 속성값을 어디서나 수정할 수 있음. private 키워드 class User { public name :string; private familyName :string; constructor(){ this.name = 'kim'; let hello = this.familyName + '안녕'; //가능 } } let 유저1 = new User(); 유저1.name = 'park'; //가능 유저1.familyName = 456; //에러 발생 class { } 안에서만 속성값 수..

www.seok.com
'캡슐화' 태그의 글 목록