这里就是把 Typescript 接口混合化
interface Person {
name: string
age: number
sex: string
}
interface dosomething {
(x: string): string
}
interface Man extends Person {
do: dosomething
}
const Man_one: Man = {
name: '张三',
age: 18,
sex: '男',
do: function (x) {
return x
},
}
console.log(Man_one.do('哈哈哈'))
interface Human {
name: string
eat(x: string): void
}
const Human_all: Human = {
name: '张三',
eat: function (x) {
console.log(x)
},
}
Human_all.eat('哈哈哈')
接口和接口继承 接口和类继承
interface Human {
name: string
eat(x: string): void
}
class Man implements Human {
constructor(name: string) {
this.name = name
}
name: string
eat() {
console.log(this.name)
}
sleep() {
console.log('睡觉了')
}
}
let Man_1 = new Man('张三')
Man_1.sleep()
Man_1.eat()
interface Human {
name: string
eat(x: string): void
}
interface Woman extends Human {
run(): void
}
const Woman_One: Woman = {
name: '小花',
eat: function (x) {
console.log(x)
},
run: function () {
console.log('这就是小花的自定义属性')
},
}
Woman_One.eat('小花的自定义属性')
Woman_One.run()
interface Human {
name: string
eat(x: string): void
}
interface Woman extends Human {
run(): void
}
interface Child {
cry(): void
}
interface Girl extends Woman, Child {}
let girl: Girl = {
name: '若曦',
eat: function (x) {
console.log(x)
},
run: function () {
console.log('若曦奔跑')
},
cry: function () {
console.log('若曦哭了')
},
}
girl.eat('若曦的名字')
girl.run()
girl.cry()
class Auto {
state = 1
}
interface AutoInterface extends Auto {}
class C implements AutoInterface {
state = 1
}
class Bus extends Auto implements AutoInterface {}