// 類繼承
class Animal {
name: string
constructor(theName: string) { this.name = theName; }
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`)
}
}
class Snake extends Animal {
constructor(name: string) { super(name)} //調(diào)用父類構(gòu)造器
move(distanceInMeters = 5) {
console.log("Slithering...")
super.move(distanceInMeters) //調(diào)用父類方法
}
}
class Horse extends Animal {
constructor(name: string) { super(name); }
move(distanceInMeters = 45) {
console.log("Galloping...");
super.move(distanceInMeters);
}
}
let sam = new Snake("Sammy the Python")
sam.move()
// Slithering...
// Sammy the Python moved 5m.
let tom :Animal = new Horse("Tommy the Palomino");
// 即使 tom被聲明為 Animal類型,但因?yàn)樗闹凳?Horse,調(diào)用 tom.move(34)時(shí),它會(huì)調(diào)用 Horse里重寫的方法
tom.move(333)
// Galloping...
// Tommy the Palomino moved 333m.
// 類成員屬性, 默認(rèn)public
// 當(dāng)成員被標(biāo)記成 private時(shí),它就不能在聲明它的類的外部訪問
class Person {
private name: string;
constructor(theName: string) { this.name = theName; }
}
class Student extends Person {
constructor() { super("Rhino"); }
}
const bob = new Student()
// bob.name error
// protected修飾符與 private修飾符的行為很相似,但有一點(diǎn)不同, protected成員在派生類中仍然可以訪問
class Person2 {
protected name: string;
constructor(name: string) { this.name = name; }
}
class Employee extends Person2 {
private department: string;
constructor(name: string, department: string) {
super(name)
// this.name = '11111'
this.department = department;
}
public getElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}
let howard = new Employee("Howard", "Sales");
console.log(howard.getElevatorPitch());
// console.log(howard.name); // 錯(cuò)誤
// 參數(shù)屬性
// 參數(shù)屬性通過給構(gòu)造函數(shù)參數(shù)前面添加一個(gè)訪問限定符來聲明。
// 使用 private限定一個(gè)參數(shù)屬性會(huì)聲明并初始化一個(gè)私有成員;對于 public和 protected來說也是一樣。
class Octopus {
readonly numberOfLegs: number = 8;
constructor(private name: string) {
}
}
const nsjd = new Octopus('bob')
//const bobName = nsjd.name //error
//存儲器
let passcode = "secret passcode2";
class Employee4 {
private _fullName: string;
get fullName(): string {
return this._fullName;
}
set fullName(newName: string) {
if (passcode && passcode == "secret passcode") {
this._fullName = newName;
}
else {
console.log("Error: Unauthorized update of employee!");
}
}
}
let employee = new Employee4();
employee.fullName = "Bob Smith"; // Error: Unauthorized update of employee!
if (employee.fullName) {
alert(employee.fullName);
}
// 類的靜態(tài)屬性
class Book{
constructor(public bookName:string){}
static getName(){
return `i am a static method`
}
}
const abc = new Book('Harry Port')
console.log(abc.bookName) //成員變量
console.log(Book.getName()) //靜態(tài)方法
// 抽象類
// 抽象類做為其它派生類的基類使用。 它們一般不會(huì)直接被實(shí)例化。
// 抽象類中的抽象方法不包含具體實(shí)現(xiàn)并且必須在派生類中實(shí)現(xiàn)
abstract class Computer{
constructor(public type:string){}
printType():void{
console.log(this.type)
}
abstract printPrice(price:number):void // 抽象方法
}
class Mac extends Computer{
constructor(type:string){
super(type)
console.log('start constructor')
}
printPrice(price:number):void{ //實(shí)現(xiàn)抽象方法
console.log(`the ${this.type} computer's price is ${price}`)
}
getSoft():void{
console.log('hello wold')
}
}
const macpro = new Mac('MacBook Pro')
macpro.printType()
macpro.printPrice(13000)
macpro.getSoft()
class Greeter {
static standardGreeting = "Hello, there";
greeting: string;
greet() {
if (this.greeting) {
return "Hello, " + this.greeting;
}
else {
return Greeter.standardGreeting;
}
}
}
let greeter1: Greeter;
greeter1 = new Greeter();
console.log(greeter1.greet());
let greeterMaker: typeof Greeter = Greeter;
greeterMaker.standardGreeting = "Hey there!";
let greeter2: Greeter = new greeterMaker();
console.log(greeter2.greet());
TypeScript-類
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- javascript提供構(gòu)造函數(shù)和原型的方式來構(gòu)造復(fù)用組件; TypeScript提供類的概念;共同點(diǎn)都要實(shí)例化;...
- TypeScript 類型兼容 Simple Private & Protected 編譯不通過,Animal的n...
- 目前前端發(fā)展得百花齊放、技術(shù)日新月異,就連各種編輯器也好用到匪夷所思,又層出不窮。近兩年來,我從Sublime T...
- 1、ts中類的定義使用關(guān)鍵字class、繼承使用 extends 和 super 2、子類方法名和父類相同表示重寫...
- 接口(Interfaces)可以用于對「對象的形狀(Shape)」進(jìn)行描述 類實(shí)現(xiàn)接口: 實(shí)現(xiàn)(implement...