当前位置:网站首页>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')
}
}
边栏推荐
- Abnova CD81 monoclonal antibody related parameters and Applications
- 当Steam教育进入个性化信息技术课程
- 【UE4】UnrealInsight获取真机性能测试报告
- Abnova CRISPR spcas9 polyclonal antibody protocol
- Selenium element information
- 【刷题记录】1. 两数之和
- Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
- Propping of resources
- 3.3 project evaluation
- Applet page navigation
猜你喜欢
![Ros2 topic [01]: installing ros2 on win10](/img/46/550945aa36d2bec03e5dd29404f409.png)
Ros2 topic [01]: installing ros2 on win10

Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms

Duchefa p1001 plant agar Chinese and English instructions

Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书

Kubernetes resource object introduction and common commands (V) - (configmap & Secret)

Duchefa丨S0188盐酸大观霉素五水合物中英文说明书

2022 Beijing eye health products exhibition, eye care products exhibition, China eye Expo held in November

haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)

教你自己训练的pytorch模型转caffe(三)

Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
随机推荐
渗透创客精神文化转化的创客教育
如何让化工企业的ERP库存账目更准确
Introduction to dead letter queue (two consumers, one producer)
Composition of applet code
Applet global configuration
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
Is it safe to open an account online? Where can I get a low commission?
Frequent MySQL operations cause table locking problems
Abnova DNA marker high quality control test program
小程序全局配置
Ros2 topic [01]: installing ros2 on win10
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
Use of thread pool
[quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system
表单文本框的使用(二) 输入过滤(合成事件)
Nprogress plug-in progress bar
Make Jar, Not War
Maker education infiltrating the transformation of maker spirit and culture
2.8 basic knowledge of project management process