接口
interface SquareConfig {
color?: string
ary?: number[]
width: number
height: number
readonly getArea: Function
}
const s1: SquareConfig = { width: 1, height: 2, ary: [1, 2], getArea: function (): number { return s1.width * s1.height } }
s1.width = 32
console.log(s1)
console.log(s1.getArea())
interface SearchFunc {
(source: string, subString: string): boolean
}
let mySearch: SearchFunc
mySearch = function (source, subString) {
let result = source.search(subString)
return result > -1
}
console.log(mySearch('aabbccdd', 'c'))
interface ClockInterface {
currentTime: Date
setTime(d: Date)
}
class Clock implements ClockInterface {
currentTime: Date
setTime(d: Date) {
this.currentTime = d
}
getTime() {
return this.currentTime
}
constructor(currentTime: Date) {
this.currentTime = currentTime
}
}
const Clock1 = new Clock(new Date('2020-06-30'))
console.log(Clock1.getTime())
interface Shape {
color: string
}
interface Square extends Shape {
sideLength: number
}
const square: Square = { color: 'blue', sideLength: 10 }
interface ClockInterfaceExtend extends Clock {
select(): void
}
const Clock2 = <ClockInterfaceExtend>{ currentTime: new Date('2019-05-28'), setTime: () => { }, getTime: () => { return Clock2.currentTime }, select: () => { } }
console.log(Clock2.getTime())
类
class Greeter {
public greeting: string
private color: string
protected timer: Date
constructor(message: string) {
this.greeting = message
}
greet() {
return "Hello, " + this.greeting
}
getColor() {
return this.color
}
setColor(color: string) {
this.color = color
}
}
class Greeter2 extends Greeter {
private greeting2: string
constructor(message: string) {
super(message)
this.greeting2 = message
}
setGreeting(message: string) {
this.greeting = message
}
getTimer() {
return this.timer
}
setTimer(timer: Date) {
this.timer = timer
}
}
const greeter2 = new Greeter2("world")
greeter2.setGreeting('Amercia')
greeter2.setColor('red')
greeter2.setTimer(new Date('2018-03-12'))
console.log(greeter2.greet())
console.log(greeter2.getColor())
console.log(greeter2.getTimer())
abstract class Department {
private readonly DepartmentName: string
constructor(public name: string) {
this.DepartmentName = name
}
printName(): void {
console.log('Department name: ' + this.DepartmentName)
}
abstract printMeeting(): void
}
class AccountingDepartment extends Department {
constructor() {
super('Accounting and Auditing')
}
printMeeting(): void {
console.log('The Accounting Department meets each Monday at 10am.')
}
generateReports(): void {
console.log('Generating accounting reports...')
}
}
const department = new AccountingDepartment()
department.printName()
department.generateReports()
Mixins
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name]
})
})
}
class Disposable {
isDisposed: boolean
dispose() {
this.isDisposed = true
}
}
class Activatable {
isActive: boolean
activate() {
this.isActive = true
}
deactivate() {
this.isActive = false
}
}
class SmartObject implements Disposable, Activatable {
isDisposed: boolean = false
dispose: () => void
isActive: boolean = false
activate: () => void
deactivate: () => void
}
applyMixins(SmartObject, [Disposable, Activatable])
const smartObj = new SmartObject()
console.log(smartObj)