Typescript(七)interface接口

1:接口作为约束与规范

我们可以根据需求来定义接口,然后我们再定义类来实现这个接口。接口为一个或多个类提供规范。

2:优化程序设计

面向对象设计中我们追求的原则之一就是高内聚,低耦合。可是类与类之间往往会有千丝万缕的关系,比如泛化、实现、组合、聚合、关联、依赖。而接口则可以将一个类对另一个类的依赖性降到最低,这就是【接口隔离】

以上是面向对象编程中接口的两个主要作用。

1:普通定义

举个例子:我们计划招聘几个女服务员,要求年龄30以下,身高165以上。

正常我们是这样实现的:

const normal = (name:string,age:number,height:number):string => {
    let str = "";
    if(age > 30 || height < 165)
    {
        str = name+"未通过审核"
    }
    else
    {
        str = name+"通过审核,身高:"+height+"年龄:"+age;
    }
    return str;
}
let result = normal('大脚',25,170);
console.log(result);   // 大脚通过审核,身高:170年龄:25

2:接口定义实现

这时候问题来了,程序开发中一直强调“代码重用”,两个方法用的类型注解一样,需要作个统一的约束。

数组定义中我们学了一个类型别名的知识可以解决代码重复的问题,这里尝试使用一下接口(Interface).

如下所示:

interface person{
    name:string,
    age:number,
    height:number
}
const liuying = {
    name:"刘英",
    age:25,
    height:177
}
const usrInterface = (liuying:person):string=>{
    let str = '';
    if(liuying.age > 30 || liuying.height < 165)
    {
        str = liuying.name+"未通过审核"
    }
    else
    {
        str = liuying.name+"通过审核,身高:"+liuying.height+"年龄:"+liuying.age;
    }
    return str;
}
let result_ying = usrInterface(liuying);
console.log(result_ying);   // 刘英通过审核,身高:177年龄:25

3:接口和类型别名的区别

现在我们学了接口,也学过了类型别名,这两个语法和用处好像一样,我先表个态,确实用起来基本一样,但是也有少许的不同。

类型别名可以直接给类型,比如string,而接口必须代表对象。

比如我们的类型别名可以写出下面的代码:

type Girl1 = stirng;

但是接口就不能这样写,它必须代表的是一个对象,也就是说,你初始化girl的时候,必须写出下面的形式.

const girl = {
  name: "大脚",
  age: 18,
  bust: 94,
};

4:接口非必选值得定义

正常接口(interface)中定义的变量是必须被继承的,但是,在正常使用的时候,很可能有一些变量是非必需的,其实typeScript已经为我们准备好了相应的办法,就是在 : 号前加一个 ?

比如把person的接口写成这样。

interface person{
    name:string,
    age:number,
    height:number,
    waistline?: number
}

上边的代码改造了一下:

interface person{
    name:string,
    age:number,
    height:number,
    waistline?: number
}
const liuying = {
    name:"刘英",
    age:25,
    height:177
}
const usrInterface = (liuying:person):string=>{
    let str = '';
    if(liuying.age > 30 || liuying.height < 165)
    {
        str = liuying.name+"未通过审核"
    }
    else
    {
        str = liuying.name+"通过审核,身高:"+liuying.height+"年龄:"+liuying.age+"腰围:"+liuying.waistline;
    }
    return str;
}
let result_ying = usrInterface(liuying);
console.log(result_ying);   
// 刘英通过审核,身高:177年龄:25腰围:undefined

5:允许加入任意值

有的时候,我们需要在对象中添加一些接口(interface)没有定义的一些值,就是自己愿意写什么就写什么。这时候interface接口也是支持的。方法如下:

interface person2{
    name:string,
    age:number,
    height:number,
    waistline?: number
    [proname:string]:any,// 属性名的类型是string,对应的值可以是任何类型
}
const liuying2:person2 = {
    name:"刘英",
    age:25,
    height:177,
    sex:"女",
    education:"博士"
}
const usrInterface2 = (liuying:person2):string=>{
    let str = '';
    if(liuying.age > 30 || liuying.height < 165)
    {
        str = liuying.name+"未通过审核"
    }
    else
    {
        str = liuying.name+"通过审核,身高:"+liuying.height+"年龄:"+liuying.age+"性别:"+liuying.sex+"学历:"+liuying.education;
    }
    return str;
}
let result_ying2 = usrInterface2(liuying2);
console.log(result_ying2);   // 刘英通过审核,身高:177年龄:25性别:女学历:博士

6:接口里的方法

接口里不仅可以存属性,还可以存方法,比如这时候有个say()方法,返回值是string类型。这时候你就不要再想成简历了,你需要更面向对象化的编程,想象成一个人。

interface person3{
    name:string,
    age:number,
    height:number,
    waistline?: number
    [proname:string]:any,// 属性名的类型是string,对应的值可以是任何类型
    say():string,//say()是一个方法,string是方法的返回值
}
 
const liuying3:person3 = {
    name:"刘英",
    age:25,
    height:177,
    sex:"女",
    education:"博士",
    say()
    {
        return "翠花,上酸菜!";
    }
}

7:接口和类的约束

Javascript ES6语法中是有类这个概念的,类可以和接口很好的结合,如下所示:

interface person3{
    name:string,
    age:number,
    height:number,
    waistline?: number
    [proname:string]:any,// 属性名的类型是string,对应的值可以是任何类型
    say():string,//say()是一个方法,string是方法的返回值
}
class classes implements person3{
    name = "大脚";
    age = 29;
    height = 186
    say()
    {
        return "翠花,上酸菜!";
    }
}

8:接口间的继承

interface person3{
    name:string,
    age:number,
    height:number,
    waistline?: number
    [proname:string]:any,// 属性名的类型是string,对应的值可以是任何类型
    say():string,//say()是一个方法,string是方法的返回值
}
class classes implements person3{
    name = "大脚";
    age = 29;
    height = 186
    say()
    {
        return "翠花,上酸菜!";
    }
}
 
interface Teacher extends person3 {
  teach(): string;
}
 
const laoshi:Teacher = {
    name:"刘英",
    age:25,
    height:177,
    sex:"女",
    education:"博士",
    say()
    {
        return "翠花,上酸菜!";
    },
    teach()
    {
        return "翠花,留作业!";
    }
}

以上大概就是接口的基本内容。

有好的建议,请在下方输入你的评论。

欢迎访问个人博客
https://guanchao.site

欢迎访问小程序:

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值