当前位置:网站首页>ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
2022-07-05 20:44:00 【qq_46302247】
类的简介
class Person{
// 没有static关键字的是实例的属性和方法,只能通过实例访问和修改、调用
name: string = 'tm' //等同于 name='tm'
age: number = 18 //等同于 age=18
// 有readonly关键字的是只读属性,不能修改
// readonly age: number = 18
sayHellow() {
console.log('hellow');
}
//有static关键字的是类属性(静态属性)和类方法(静态方法),只能通过类名访问和修改、调用
static sex: string = '女' //等同于 static sex='女'
// readonly修饰类属性时,必须放在static后面
// static readonly sex: string = '女'
static sayHellow() {
console.log('static--hellow');
}
}
console.log(Person.sex);//女
Person.sayHellow() //static--hellow
const person1 = new Person()
console.log(person1.name, person1.age); //tm 18
person1.sayHellow()//hellow
构造函数和它的this
class Dog{
name: string
age: number
// this永远指向当前实例对象
constructor(name: string, age: number) {
this.name = name
this.age = age
}
// this永远指向当前调用实例对象
bark() {
console.log(this.name);
}
}
//一旦使用new创建对象,就会调用Dog的constructor方法
const dog1 = new Dog('小黑', 3)
const dog2 = new Dog('小白', 6)
dog1.bark() //小黑
dog2.bark() //小白
继承和抽象类
//abstract关键字修饰的类是抽象类,抽象类是专门用来被继承的类,不能用来创建对象
abstract class Animal{
name: string
age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
//abstract关键字修饰的方法是抽象方法,抽象方法没有方法体,抽象方法只能定义在抽象类中,子类必须对抽象方法进行重写
abstract sayHellow():void
}
// 如果使用了extends关键字,则Animal是父类,Dog是子类;相当于将父类的属性和方法都复制了一份到子类中
class Dog extends Animal{
// 子类也可以自己新增方法和属性
sex: string
constructor(name: string, age: number, sex: string) {
super(name, age)//调用父类的构造函数
this.sex = sex
}
sayHellow() {
console.log('狗在叫');
}
run() {
console.log('狗在跑');
}
}
class Car extends Animal{
//父类方法的重写
sayHellow() {
console.log('猫在叫');
}
}
const dog = new Dog('狗狗', 5, '女')
console.log(dog.sex); //女
dog.sayHellow()//狗在叫
dog.run()//狗在跑
const car = new Car('猫猫', 2)
car.sayHellow()//猫在叫
接口
//接口中的所有属性都不能有实际值,所有方法都是抽象方法
//接口主要是定义某个标准,限制类必须去符合这个标准
//接口是ts特有的,js中没有接口的概念
interface myInter{
name: string
sayHellow():void //该函数没有返回值
}
class MyClass implements myInter{
name: string
constructor(name: string) {
this.name = name
}
sayHellow() {
console.log('aaa')
}
}
边栏推荐
- Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
- Use of thread pool
- 小程序页面导航
- Applet event binding
- go 文件路径操作
- Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
- [Yugong series] go teaching course in July 2022 004 go code Notes
- Nprogress plug-in progress bar
- Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
- 2.8 basic knowledge of project management process
猜你喜欢
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
解析创客教育的知识迁移和分享精神
Abnova DNA marker high quality control test program
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
Prosci LAG-3 recombinant protein specification
Typhoon is coming! How to prevent typhoons on construction sites!
Abnova丨血液总核酸纯化试剂盒预装相关说明书
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
Abnova丨CRISPR SpCas9 多克隆抗体方案
线程池的使用
随机推荐
Duchefa MS medium contains vitamin instructions
Leetcode (347) - top k high frequency elements
Common view container class components
phpstudy小皮的mysql点击启动后迅速闪退,已解决
Propping of resources
Use of thread pool
Abnova fluorescent dye 620-m streptavidin scheme
小程序全局配置
研学旅游实践教育的开展助力文旅产业发展
鸿蒙os第四次学习
Classic implementation method of Hongmeng system controlling LED
Which is the best online collaboration product? Microsoft loop, notion, flowus
培养机器人教育创造力的前沿科技
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
Interpreting the daily application functions of cooperative robots
Typhoon is coming! How to prevent typhoons on construction sites!
MySQL fully parses json/ arrays
When steam education enters personalized information technology courses
教你自己训练的pytorch模型转caffe(二)
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现