类的使用
class Greeter {
greeting: string
constructor ( message: string ) { this.greeting = message }
greet () {
return "Hello" + this.greeting
}
}
const greeter = new Greeter( "world" )
// 使用new 构造了 Greeter 类的实例. 它会调用之前定义的构造函数,创建一个Greeter类型的新对象,并执行构造函数初始化它
继承的用例
PS:你在你公司上班,公司的设备,你也都可以使用,大概就叫继承
class Animal {
move( distanceInMeters: number = 0 ) {
console.log(`Animal moved ${distanceInMeters}m.`)
}
}
class Pig extends Animal {
bark () {
console.log('heng heng')
}
}
const pig = new Pig()
pig.bark() // heng heng
pig.move(20) // Animal moved 20m.
pig.bark() // heng heng
官网解释:类从基类中继承了属性和方法。 这里, Pig是一个 派生类,它派生自 Animal 基类,通过 extends关键字。 派生类通常被称作 子类,基类通常被称作 超类
继承拓展2
class Animal { // 基类 or 超类 鸡肋 = 超累
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) }
move(distanceInMeters = 5) {
console.log("Slithering...")
super.move(distanceInMeters)
}
}
class Horse extends Animal {
constructor(name: string) { super(name) }
move(distanceInMeters = 20) {
console.log("Galloping...")
super.move(distanceInMeters)
}
}
let sam = new Snake("Sammy the TypeScript")
let tom:Animal = new Horse("Tommy the Palomino")
sam.move()
tom.move(33)
// Slithering...
// Sammy the TypeScript moved 5 m.
// Galloping...
// Tommy the Palomino moved 33m.
PS: 派生类包含了一个构造函数,它 必须调用 super(),它会执行基类的w构造函数。 而且,在构造函数里访问 this的属性之前,我们 一定要调用 super()。 相当于super调用的是基类的方法。
公共,私有与受保护的修饰符
1、public
// 在别的语言之中,必须明确使用public表示该成员是课件的。在Ts中 默认都是public
class Animal {
public name: string
public construstor(theName: string) { this.name = theName }
public move(distanceInMeters: number) {
console.log(`${this.name} moved ${distanceInMeters}m.`)
}
}
2、private
class Animal {
private name: string
construstor(theName: string) { this.name = theName }
}
let animal = new Animal("Julius")
animal.name = "Mayun" // error name为私有的
// 当成员被 private 标记时,它就不能在声明它的类 外部访问
class Animal {
private name: string
construstor(theName: string) { this.name = theName }
}
class Rhino extends Animal {
cosntrustor() { super("Rhino") }
}
class Employee {
private name: string
construstor (theName: string) { this.name = theName }
}
let animal = new Animal("Goat") // name: Goat
let rhino = new Rhino() // name: Rhino
let employee = new Employee("Bob") // name: Bob
animal = rhino // success
animal = employee // error Animal 与 Employee 不兼容
// 两者的 name 来着同一处声明的时候,才是兼容的。
3、protected
// protected修饰符与 private修饰符的行为很相似,但有一点不同, protected成员在派生类中仍然可以访问。
class Person {
protected name: string
construstor(name: string) { this.name = name }
}
class Employee extends Person {
private department: string
constructor(name: string, department: string) {
super(name) // 调用父类
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()) // Hello, my name is Howard and I work in Sales
console.log(howard.name) // error 我们不能在 Person类外使用 name,但是我们仍然可以通过 Employee类的实例方法访问,因为 Employee是由 Person派生而来的。
未完待续,下班回家补稿。