接口(interface)使用方法:
1.接口:规定类必须具有的功能
2.接口没有构造函数
3.接口中的方法不能有方法体,必须写上返回值类型
写法:
interface A {
//接口没有构造函数
num1: number
//接口中的方法不能有方法体,必须写上返回值类型
a1(): void;
}
4.接口不能被实例化
5.实现类:必须有接口中的属性,必须重写接口中的方法
写法:
class B implements A {
num1: number
a1(): void {
console.log('重写的接口的函数');
}
constructor(num1: number) {
this.num1 = num1
}
}
let bb: B = new B(1);
bb.a1()
练习:
1.写Usb
interface Usb {
in(): void;
outs(): void;
}
class Fs implements Usb {
type: string
ts() {
console.log('调节风扇速度');
}
constructor(type: string) {
this.type = type
}
in(): void {
console.log('风扇插入Usb');
}
outs(): void {
console.log('风扇拔出Usb');
}
}
let fss: Fs = new Fs('海尔')
fss.in()
fss.ts()
fss.outs()
2.类型 滚动 容量 写数据 读数据
interface Gd {
in(): void;
outs(): void;
}
class fs implements Gd {
type: string
rl() {
console.log('滚动');
}
constructor(type: string) {
this.type = type
}
in(): void {
console.log('写数据');
}
outs(): void {
console.log('读数据');
}
}
let fss1: fs = new fs('鼠标')
fss1.in()
fss1.rl()
fss1.outs()
3.门锁
class Door {
openDoor() {
console.log('开门');
}
closeDoor() {
console.log('关门');
}
}
interface Locks {
openLock(): void
closeLock(): void
}
interface Ring {
playRing(): void
}
class LockDoor extends Door implements Locks, Ring {
openLock(): void {
console.log('开锁');
}
closeLock(): void {
console.log('上锁');
}
playRing(): void {
console.log('铃响了.....');
}
}
let lc: LockDoor = new LockDoor();
lc.openLock()
lc.openDoor()
lc.playRing()
lc.closeDoor()
lc.closeLock()
4.手机
普通手机 型号 品牌 打电话 发短信 普通手机+音乐
智能手机 播放音乐 视频 上网 拍照
普通手机写法:
class Phone {
brand: string
type: string
constructor(brand: string, type: string) {
this.brand = brand
this.type = type
}
call() {
console.log('打电话');
}
sendInfo() {
console.log('发短信');
}
info() {
console.log(`${this.brand}品牌的${this.type}的手机`);
}
}
interface Music {
playMusic(sing: string): void
}
interface Video {
playVideo(Video: string): void
}
interface InterNat {
playInterNat(net: string): void;
}
interface Image {
playImage(): void
}
//普通手机
class PtPhone extends Phone implements Music {
constructor(brand: string, type: string) {
super(brand, type)
}
playMusic(sing: string): void {
console.log(`正在播放音乐:${sing}`);
}
}
let pt: PtPhone = new PtPhone('诺基亚', '3200')
pt.info()
pt.sendInfo()
pt.call()
pt.playMusic('鸡你太美')
智能手机:
//智能手机
class AiPhone extends Phone implements Music, Video, InterNat, Image {
constructor(brand: string, type: string) {
super(brand, type)
}
playMusic(sing: string): void {
console.log(`正在播放音乐:${sing}`);
}
playVideo(Video: string): void {
console.log(`正在播放视频:${Video}`);
}
playInterNat(_net: string): void {
console.log(`连接网络:${1}`);
}
playImage(): void {
console.log('已拍照');
}
}
let ai: AiPhone = new AiPhone('苹果', '15')
ai.info()
ai.sendInfo()
ai.call()
ai.playMusic('起风了')
5.电脑:
计算机 电脑品牌 型号,cpu cpu品牌,主频 介绍,硬盘 容量 介绍,内存 容量 介绍
//电脑
class Computer {
brand: string
type: string
constructor(brand: string, type: string) {
this.brand = brand
this.type = type
}
info() {
console.log(`${this.brand}品牌的${this.type}的电脑`);
}
}
interface Cp {
Cpubrand(brand: string): void
cpuZP(ZP: string): void
}
interface yp {
ypRl(Rl: string): void
}
interface nc {
ncrl(ncrl: string): void
}
class HCComputer extends Computer implements Cp, yp, nc {
constructor(brand: string, type: string) {
super(brand, type)
}
Cpubrand(brand: string): void {
console.log(`Cpu的品牌是:${brand}`);
}
cpuZP(ZP: string): void {
console.log(`主频是${ZP}`);
}
ypRl(Rl: string): void {
console.log(`硬盘容量是${Rl}`);
}
ncrl(ncrl: string): void {
console.log(`内存容量是${ncrl}`);
}
}
let hp:HCComputer=new HCComputer('英特尔','i9')
hp.info()
hp.Cpubrand('Intel')
hp.cpuZP('3.8GHz')
hp.ypRl('512GB')
hp.ncrl('16GB')