四、typescript类的使用

2023-12-29 09:06:58

1. es6中类的用法:
(1)属性和方法:使用class定义类,使用construction定义构造函数
通过new生成新实例的时候。会自动调用构造函数

class Animal{
    public name;
    constructior(name){
        this.name = name
    }
    sayHi(){
        return this.name
    }
}
let a = new Animal('jack')
a.sayHi()  //jack

(2)类的继承
使用extends关键字实现继承,子类使用super关键字来调用父类的构造函数和方法

class  child extends Animal{
    constructor(name){
        spuer(name)
    }
    sayHi(){
        return 'woshi '+ super.sayHi()
    }
}
let a = new child('child')
a.sayHi() //woshi child

(3)存取器

class Animal{
    constructor(name){
        this.name = name
    }
    get name(){
        return 'jack'
    }
    set name(val){
        console.log('setting::'+val)
    }
}
let a = new Animal('kitty')  //setting::kitty
a.name = 'tom'  //setting::tom
console.log(a.name)  //jack

(4)静态方法 static

class Animal{
    static isAnimal(a){
        return   a instanceof Animal
    }
}
let a = new Animal('jack'')
Animal.isAnimal(a)  //true
a.isAnimal(a)  //error

2. es7中类的用法
(1)实例属性
es6中实例的属性只能通过构造函数中的this.xxx来定义,ES7 提案中可以直接在类里面定义:

class Animal {
    name = 'jack';
    constuctor(){}}

let a = = new Animal()
a.name   //jack

(2) 静态属性

class  Animal{
    static num = 2;
    constructor(){}
}
Animal.num   //2

3. typeScript中类的用法
public private 和 protected

TypeScript 可以使用三种访问修饰符(Access Modifiers),分别是 public、private 和 protected。
public 修饰的属性或方法是公有的,可以在任何地方被访问到,默认所有的属性和方法都是 public 的
private 修饰的属性或方法是私有的,不能在声明它的类的外部访问,子类也不能访问
protected 修饰的属性或方法是受保护的,它和 private 类似,区别是它在子类中也是允许被访问的
public

class Animal {
  public name;
  public constructor(name) {
    this.name = name;
  }}

let a = new Animal('Jack');console.log(a.name); // Jack
a.name = 'Tom';console.log(a.name); // Tom
private
class Animal {
  private name;
  public constructor(name) {
    this.name = name;
  }}

let a = new Animal('Jack');console.log(a.name); // Jack
a.name = 'Tom';  //error  不能访问
protected:该类只允许继承

class Animal{
    public name;
    protected constructor(name){
        this.name = name
    }
}

class Cat extends Animal{
    constructor(name){
        super(name)
    }
}
let a = new Animal('jack')  //error Animal的构造函数是受保护的,只能在类的声明中访问
let a = new Cat('name')  //ok

参数属性:readonly

class Animal{
    readonly name'
    public constructor(public readonly name){
    }
}

抽象类:abstract
抽象类不允许被实例化:

abstract class Animal{
    public name ;
    public constructor(name){
    }
    public abstract sayHi()
}
let a = new  Animal('')  //error

类:

class Greeter{
    constructor(msg:string){
        this.message=msg
    }
    message:string;
    getMsg(){
        return 'msg::'+this.message
    }
}
let msg = new Greeter('xiaoxi')
console.log(msg)

继承:

class Parent{
    name:String;
    constructor(newName:string){
        console.log('p name',newName)
        this.name = newName
    }
    move(moved:number=0){
        console.log('p move',moved)
    }
}
class Child1 extends Parent{
    constructor(name:string){
        console.log('chidl1name',name)
        super(name)
    }
    move(moved=11){
        console.log('child1',moved)
        super.move(moved)
    }
}
class Child2 extends Parent{
    constructor(name:string){
        console.log('chid2name',name)
        super(name)
    }
    move(moved = 22){
        console.log('child2')
        super.move(moved)
    }
}
let my1 = new Child1('child1')
let my2:Parent = new Child2('child2')
console.log(my1,my2)
my1.move()
my2.move(10)

公共,私有与受保护的修饰符
默认为public公共的
private 私有的
protected:与 private修饰符的行为很相似,但有一点不同, protected成员在派生类中仍然可以访问

class P{
    private name:string ;  //私有的
    public width:number;
    constructor(newName,width=100){
        this.name = newName
        this.width = width
    }
}
new P('name').name    //error name是私有的
new P(200).width   //ok

class P

P1 {
    private name: string;
    constructor(name) {
        this.name = name
    }
}
class P1Child extends P1 {
    constructor() {
        super('p1-child-name')
    }
}
class P2 {
    private name: string;
    constructor(name) {
        this.name = name
    }
}
let aa = new P1('p1-name')
let bb = new P1Child()
let dd = new P2('p2-name')
console.log(aa,bb,dd)
aa = bb
aa = dd  //error 不兼容

protected:

class P3{
    protected name:string;
    protected constructor(name){
        this.name =name
    }
}
class C1 extends P3{
    private cname:string;
    constructor(name:string,cname:string){
        super(name)
        this.cname = cname
    }
    public getName(){
        return `${this.name}----${this.cname}`
    }
}
let res1 = new C1('name','cname')
console.log('res1',res1)
// let res2 = new P3('p3name')  //error p3的构造函数是被保护的 仅能在类的声明中访问

readonly修饰符:将关键字设置成只读的,只读属性必须在声明时或在构造函数里初始化

class P{
    readonly name:string;
    
    constructor(name:string, readonly name2:string){
        this.name =name
    }
}

存取器

class P{
    name:string
}
let res = new P()
res.name='name'
if(res.name){ console.log(res.name) }


get() set()
 class P{
    private name:string;
    set name(newName:string){
        if(true){
            console.log(newName)
        }else{
            console.log('error')
        }
    }   
    get name():string{
        return name
        
    }
 }

静态属性 static:

class P4 {
    static o = {x:0,y:0}
    change(init:{x:number;y:number}){
        // P4.o.x  //访问静态
        return (init.x-P4.o.x)* (init.y-P4.o.y)/this.scale
    }
    constructor(public scale:number){
    }
}
let grid1 = new P4(1.0)
let grid2 = new P4(5.0)
console.log(grid1.change({x:20,y:20}))

抽象类 abstract:关键字用于定义抽象类和在抽象类内部定义抽象方法
抽象类中的抽象方法不会包含具体实现并且必须在派生类中实现,和接口方法语法相似,两者都是定义方法千米不包含方法体,而抽象方法必须包含abstract关键字并且可以包含访问修饰符

abstract class P{
    abstract fun():void;
    move():void{}
}

抽象类:

abstract class P5{
    constructor(public name:string){}
    printName():void{
        console.log('p-n'+this.name)
    }
    abstract printNewStr():void  //必须在派生类中实现
}
class P5_child extends P5{
    constructor(){
        super('child-name')
    }
    printNewStr():void{
        console.log('抽象方法')
    }
    report():void{
        console.log('report...')
    }
}
let res5 : P5;  //创建一个抽象类型的引用
res5 = new P5() //error 不能创建一个抽象类的实例
res5 = new P5_child()  //ok  允许对一个抽象子类进行实例化和赋值
res5.printName()
res5.printNewStr()
res5.report()  //error  方法在声明的抽象类中不存在

文章来源:https://blog.csdn.net/weixin_43857653/article/details/135226923
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。