当前位置:网站首页>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 fluorescent dye 620-m streptavidin scheme
- Abnova丨荧光染料 620-M 链霉亲和素方案
- Where is a good stock account? Is online account manager safe to open an account
- 清除app data以及获取图标
- Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
- Interpreting the daily application functions of cooperative robots
- Nprogress plug-in progress bar
- 2.8 basic knowledge of project management process
- National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
- Is the securities account given by the school of Finance and business safe? Can I open an account?
猜你喜欢
随机推荐
Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书
小程序页面导航
Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
Duchefa low melting point agarose PPC Chinese and English instructions
Dry goods navigation in this quarter | Q2 2022
Model method
鸿蒙os第四次学习
Abnova cyclosporin a monoclonal antibody and its research tools
Maker education infiltrating the transformation of maker spirit and culture
[UE4] unrealinsight obtains the real machine performance test report
Ros2 topic [01]: installing ros2 on win10
小程序代码的构成
Y57. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (30)
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
Selenium element information
Applet event binding
Schema and model
NPDP如何续证?操作指南来了!
Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree