Access modifier (access modifier)
Trong các ngôn ngữ như Java hay PHP, có thể chỉ định private, protected, public cho field và method. JavaScript cũng có spec Private class fields để thực hiện property kiểu private, nhưng hơi khác với access modifier kiểu Java. TypeScript có access modifier theo phong cách Java.
| Access modifier | Mô tả |
|---|---|
| (không khai báo) | Tương đương với public |
| public | Có thể truy cập từ bất kỳ đâu |
| protected | Có thể truy cập từ class hiện tại và subclass |
| private | Chỉ có thể truy cập từ class hiện tại |
Khi bỏ qua access modifier, mặc định là public.
Access modifier có thể khai báo cho field, constructor và method.
public
Access modifier public có thể truy cập từ bất kỳ đâu. Khi bỏ qua access modifier cũng được xem là tương đương với public.
tsclassAnimal {publicname : string; // Public access modifier cho field// Public access modifier cho constructorpublic constructor(theName : string) {this.name =theName ;}// Public access modifier cho methodpublicmove (distanceInMeters : number) {console .log (`${this.name } moved ${distanceInMeters }m.`);// Có thể sử dụng `this.name` vì nó có public access modifier}}
tsclassAnimal {publicname : string; // Public access modifier cho field// Public access modifier cho constructorpublic constructor(theName : string) {this.name =theName ;}// Public access modifier cho methodpublicmove (distanceInMeters : number) {console .log (`${this.name } moved ${distanceInMeters }m.`);// Có thể sử dụng `this.name` vì nó có public access modifier}}
Implement gorilla và kiểm tra hoạt động.
tsconstgorilla = newAnimal ("Gorilla");gorilla .move (10);gorilla .name = "BigGorilla";gorilla .move (20);
tsconstgorilla = newAnimal ("Gorilla");gorilla .move (10);gorilla .name = "BigGorilla";gorilla .move (20);
Property name được khai báo public nên có thể đọc và ghi từ biến instance (gorilla). Có thể thay đổi từ "Gorilla" sang "BigGorilla".
protected
Access modifier protected có thể truy cập từ class hiện tại và subclass.
Thay đổi access modifier của method move trong class Animal từ public sang protected để xem lỗi.
tsclassAnimal {publicname : string;public constructor(theName : string) {this.name =theName ;}// Thay đổi từ `public` sang `protected`protectedmove (distanceInMeters : number) {console .log (`${this.name } moved ${distanceInMeters }m.`);}}constgorilla = newAnimal ("Gorilla");Property 'move' is protected and only accessible within class 'Animal' and its subclasses.2445Property 'move' is protected and only accessible within class 'Animal' and its subclasses.gorilla .(10); move
tsclassAnimal {publicname : string;public constructor(theName : string) {this.name =theName ;}// Thay đổi từ `public` sang `protected`protectedmove (distanceInMeters : number) {console .log (`${this.name } moved ${distanceInMeters }m.`);}}constgorilla = newAnimal ("Gorilla");Property 'move' is protected and only accessible within class 'Animal' and its subclasses.2445Property 'move' is protected and only accessible within class 'Animal' and its subclasses.gorilla .(10); move
Method gorilla.move() được khai báo protected nên chỉ có thể truy cập từ class hiện tại và subclass. Tức là truy cập từ instance gorilla bị từ chối và xảy ra lỗi compile.
Implement lại method move() được bảo vệ bằng protected để tạo gorilla di chuyển nhanh gấp 10 lần.
tsclassAnimal {publicname : string;public constructor(theName : string) {this.name =theName ;}// Thay đổi từ `public` sang `protected`protectedmove (distanceInMeters : number) {console .log (`${this.name } moved ${distanceInMeters }m.`);}}classGorilla extendsAnimal {move (distanceInMeters : number) {super.move (distanceInMeters * 10);}}constgorilla = newGorilla ("FastGorilla");gorilla .move (10);
tsclassAnimal {publicname : string;public constructor(theName : string) {this.name =theName ;}// Thay đổi từ `public` sang `protected`protectedmove (distanceInMeters : number) {console .log (`${this.name } moved ${distanceInMeters }m.`);}}classGorilla extendsAnimal {move (distanceInMeters : number) {super.move (distanceInMeters * 10);}}constgorilla = newGorilla ("FastGorilla");gorilla .move (10);
Định nghĩa class Gorilla có superclass Animal và implement move(). Trong method move() của class Gorilla, sử dụng từ khóa super để gọi method move() của superclass.
private
Access modifier private chỉ có thể truy cập từ class hiện tại.
Thay đổi protected move() thành private move(). Do thay đổi thành private, class Gorilla không được phép truy cập super.move và xảy ra lỗi.
tsclassAnimal {publicname : string;public constructor(theName : string) {this.name =theName ;}// Thay đổi từ `public` sang `private`privatemove (distanceInMeters : number) {console .log (`${this.name } moved ${distanceInMeters }m.`);}}classClass 'Gorilla' incorrectly extends base class 'Animal'. Property 'move' is private in type 'Animal' but not in type 'Gorilla'.2415Class 'Gorilla' incorrectly extends base class 'Animal'. Property 'move' is private in type 'Animal' but not in type 'Gorilla'.extends Gorilla Animal {move (distanceInMeters : number) {super.Property 'move' is private and only accessible within class 'Animal'.2341Property 'move' is private and only accessible within class 'Animal'.( move distanceInMeters * 10);}}
tsclassAnimal {publicname : string;public constructor(theName : string) {this.name =theName ;}// Thay đổi từ `public` sang `private`privatemove (distanceInMeters : number) {console .log (`${this.name } moved ${distanceInMeters }m.`);}}classClass 'Gorilla' incorrectly extends base class 'Animal'. Property 'move' is private in type 'Animal' but not in type 'Gorilla'.2415Class 'Gorilla' incorrectly extends base class 'Animal'. Property 'move' is private in type 'Animal' but not in type 'Gorilla'.extends Gorilla Animal {move (distanceInMeters : number) {super.Property 'move' is private and only accessible within class 'Animal'.2341Property 'move' is private and only accessible within class 'Animal'.* 10); move (distanceInMeters }}
Cách sử dụng phổ biến của private method là tách code dài trong class thành các phần theo chức năng.
Thay đổi access modifier
Khi kế thừa class, có thể thay đổi access modifier của method. Tuy nhiên không thể tự do thay đổi bất kỳ, chỉ có thể thay đổi theo hướng nới lỏng hạn chế truy cập. Tức là có thể thay đổi theo hướng protected > public nhưng không thể làm ngược lại.
tsclassProtectedClass {protecteddoNothing (): void {console .log ("DO NOTHING");}}classPublicClass extendsProtectedClass {publicdoNothing (): void {console .log ("DO NOTHING");}}
tsclassProtectedClass {protecteddoNothing (): void {console .log ("DO NOTHING");}}classPublicClass extendsProtectedClass {publicdoNothing (): void {console .log ("DO NOTHING");}}
Không thể implement ngược lại public > protected.
tsclassPublicClass {publicdoNothing (): void {console .log ("DO NOTHING");}}classClass 'ProtectedClass' incorrectly extends base class 'PublicClass'. Property 'doNothing' is protected in type 'ProtectedClass' but public in type 'PublicClass'.2415Class 'ProtectedClass' incorrectly extends base class 'PublicClass'. Property 'doNothing' is protected in type 'ProtectedClass' but public in type 'PublicClass'.extends ProtectedClass PublicClass {protecteddoNothing (): void {console .log ("DO NOTHING");}}
tsclassPublicClass {publicdoNothing (): void {console .log ("DO NOTHING");}}classClass 'ProtectedClass' incorrectly extends base class 'PublicClass'. Property 'doNothing' is protected in type 'ProtectedClass' but public in type 'PublicClass'.2415Class 'ProtectedClass' incorrectly extends base class 'PublicClass'. Property 'doNothing' is protected in type 'ProtectedClass' but public in type 'PublicClass'.extends ProtectedClass PublicClass {protecteddoNothing (): void {console .log ("DO NOTHING");}}