当前位置:网站首页>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 blood total nucleic acid purification kit pre installed relevant instructions
- Use of thread pool
- 台风来袭!建筑工地该如何防范台风!
- [UE4] unrealinsight obtains the real machine performance test report
- 2.8、项目管理过程基础知识
- Y57. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (30)
- 产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
- Duchefa cytokinin dihydrozeatin (DHZ) instructions
- 珍爱网微服务底层框架演进从开源组件封装到自研
- Common view container class components
猜你喜欢
线程池的使用
教你自己训练的pytorch模型转caffe(一)
Abbkine BCA法 蛋白质定量试剂盒说明书
CVPR 2022 | common 3D damage and data enhancement
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions
AI automatically generates annotation documents from code
Abnova DNA marker high quality control test program
14、Transformer--VIT TNT BETR
Introduction to dead letter queue (two consumers, one producer)
Applet event binding
随机推荐
Prosci LAG-3 recombinant protein specification
When steam education enters personalized information technology courses
证券开户选择哪个证券比较好?网上开户安全么?
常用的视图容器类组件
Applet project structure
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
Maker education infiltrating the transformation of maker spirit and culture
Abbkine BCA法 蛋白质定量试剂盒说明书
Usaco3.4 "broken Gong rock" band raucous rockers - DP
CCPC 2021 Weihai - G. shinyruo and KFC (combination number, tips)
2.8、项目管理过程基础知识
National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
[Yugong series] go teaching course in July 2022 004 go code Notes
Material Design组件 - 使用BottomSheet展现扩展内容(二)
Simple understanding of interpolation search
Ros2 topic [01]: installing ros2 on win10
Chemical properties and application instructions of prosci Lag3 antibody
Abnova丨血液总核酸纯化试剂盒预装相关说明书
Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
清除app data以及获取图标