当前位置:网站首页>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')
}
}
边栏推荐
- Redis唯一ID生成器的实现
- 2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
- 当Steam教育进入个性化信息技术课程
- Sort and projection
- 资源道具化
- Cutting edge technology for cultivating robot education creativity
- When steam education enters personalized information technology courses
- Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
- Specification of protein quantitative kit for abbkine BCA method
- 2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
猜你喜欢

渗透创客精神文化转化的创客教育

Leetcode (695) - the largest area of an island

Classic implementation of the basic method of intelligent home of Internet of things

Duchefa d5124 md5a medium Chinese and English instructions

Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs

ProSci LAG3抗体的化学性质和应用说明
![[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](/img/1d/22bf47bfa30b9bdc2e8fd348180f49.png)
[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

Applet event binding

Norgen AAV extractant box instructions (including features)

14、Transformer--VIT TNT BETR
随机推荐
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书
Make Jar, Not War
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
解析五育融合之下的steam教育模式
AI automatically generates annotation documents from code
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions
Abnova丨血液总核酸纯化试剂盒预装相关说明书
Duchefa丨D5124 MD5A 培养基中英文说明书
How to open an account online for futures? Is it safe?
Duchefa d5124 md5a medium Chinese and English instructions
Monorepo管理方法论和依赖安全
MySQL fully parses json/ arrays
Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Abnova丨DNA 标记高质量控制测试方案
鸿蒙os第四次学习
解读协作型机器人的日常应用功能
Relationship between mongodb documents