Typescript的interface接口类型,类的继承,(private,protected,public三种访问类型)

「这是我参与11月更文挑战的第2天,活动详情查看:2021最后一次更文挑战

interface 接口类型

只接收字符串或方法,跟type类型别名有一定区别,type可以直接声明类型

interface Person {
  	// readonly name: string				// readonly 只读类型,不可写
    name: string
    age?: number										// ?: 可有可无
    [propName: string]: any					// 除了name和age,别的类型,只要键是string值是any就行
    say(): string										// 可以存在方法
}

interface Teacher extends Person {		// 类型继承
 	teach(): string 
}

interface Sayhi {					// 定义函数类型
 	(word: string): string
}
    
const getPersonName = (person: Person): void => {
  	console.log(person.name)
}
  
const setPersonName = (person: Person, name: string): void => {
  	person.name = name
}
  
const person = {
  	name: 'dell',
  	sex: "male",
  	say() {
      	return "say hello"
    }
}

getPersonName(person)
setPersonName(person, 'lee')

class User implements Person {		// 定义user这个类去implements(应用)person这个接口
  	name: "lee"
    say() {
      	return "say hello"
    }
}

const say: Sayhi = (word: string) => {
  	return word
}

类的继承

class person {
 	name = "dell"
	getName() {
    	return this.name
  }
}

class Teacher extends person {
 	getTeacherName() {
   	return "lee" 
  }
  // super 继承父类的方法等
  getName() {
   	return super.getName() + "lee" 
  }
}

const person = new Teacher()
console.log(person.getTeacherName())  // lee
console.log(person.getName())  // delllee

private, protected, public 三种访问类型

public公共调用方法

允许在类的内外均可被调用

// public      允许在类的内外被调用
class Person {
    public name: string
    public sayHi() {
        console.log(this.name)  // 在类的内部 被调用
        console.log("hi")
    }
}

// 在类的外部被调用
const person = new Person()
person.name = "dell"
console.log(person.name)  // dell
person.sayHi()      // hi

private

只允许在类的内部被调用,如果在类的外部调用就是错误的

// private, protected, public  三种访问类型

// private      允许在类内被使用  类外调用就是错误
class Person {
    private name: string
    public sayHi() {
        console.log(this.name)  // 在类的内部 被调用 dell
        console.log("hi")
    }
}

// 在类的外部被调用
const person = new Person()
person.name = "dell"        // 报错,无法编译
console.log(person.name)  //  报错,无法编译
person.sayHi()      // hi

protected

与private相反,只允许在类内及继承的子类中使用

// private, protected, public  三种访问类型

// public       允许我在类的内外被调用
// private      允许在类内被使用  类外调用就是错误
// protected    允许在类内及继承的子类中使用
class Person {
    protected name: string
    public sayHi() {
        console.log(this.name)  // 在类的内部 被调用 dell
        console.log("hi")
    }
}

class Teacher extends Person {
    public sayBye() {
        console.log(this.name)  // dell
    }
}

// 在类的外部被调用
const person = new Person()
person.name = "dell"        // 报错,无法编译
console.log(person.name)  //  报错,无法编译
person.sayHi()      // hi

最后

公众号:小何成长,佛系更文,都是自己曾经踩过的坑或者是学到的东西

有兴趣的小伙伴欢迎关注我哦,我是:何小玍。大家一起进步鸭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值