# TypeScript Class 類別與繼承 TypeScript 支援三種存取修飾詞 ***public*** - 從任何地方都能存取,這是預設的存取層級 ***protected*** - 可從此類別及其子類別的實體存取 ***private*** - 僅可從此類別的實體進行存取 ***abstract*** - 無法建立實體 `new Position()` ```typescript type Color = 'Black' | 'White' type File = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' type Rank = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 class Position { constructor( private file: File, private rank: Rank ) { } } // 使用 abstract 無法建立實體 new Position2("A", 1) abstract class Position2 { constructor( private file: File, private rank: Rank ) { } } class Piece { protected position: Position constructor( private readonly color: Color, file: File, rank: Rank ) { this.position = new Position(file, rank) } } ``` ### implements class 使用 interface ```typescript interface UserFace { readonly name: string eat(food: string): void sleep(hours: number): void } interface Feline { meow(): void } class CatCat implements UserFace, Feline { name = 'Whiskers' eat(food: string) { console.info('Ate some', food, '. Mmm!') } sleep(hours: number) { console.info('Slept for', hours, 'hours') } meow() { console.info('Meow') } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up