class类

1.类的简介

/*
* js中使用class关键字定义一个类
* 主要包含两个部分(属性及方法)
* 定义属性
* 前面加 readonly 表示只读属性,无法修改
* 直接定义的属性是实例属性,需要通过对象的实例去访问,如 console.log(per.name);
* 在属性前使用static关键字可以定义类属性(静态属性),可以直接访问,如 console.log(Person.age);
* 定义方法
* 同理 直接定义的方法是实例方法,需要通过对象的实例去访问,如 per.say();
* 在方法前使用static关键字可以定义类方法(静态方法),可以直接访问,如 Person.say();
 */
class Person {
    //定义属性
    //直接定义的属性是实例属性,需要通过对象的实例去访问
    //定义实例属性,必须new之后才可访问 如 console.log(per.name);
    a: string = "我是实例属性";
    //在属性前使用static关键字可以定义类属性(静态属性),只能直接访问,如 
    console.log(Person.age);
    static b: string = "我是静态属性";
    //前面加 readonly 表示只读属性,无法修改
    readonly c: string = "我是只读属性";
    static readonly d: string = "我是只读静态属性";

    //定义方法
    // 实例方法,只能通过对象的实例去访问,如 per.say();
    fun1(): void {
        console.log("我是实例方法!");
    }

    // 在方法前使用static关键字可以定义类方法(静态方法),只能直接访问,如 Person.say()
    static fun2(): void {
        console.log("我是静态方法!");
    }
}

const per = new Person();
per.fun1();// 我是实例方法!
Person.fun2();// 我是静态方法!
console.log(per.a);//我是实例属性
per.a = "我是实例属性--小明";
console.log(per.a);//我是实例属性--小明
console.log(Person.b);//我是静态属性
Person.b = "我是静态属性--小明";
console.log(Person.b);//我是静态属性--小明

2.构造函数

/*
* 构造函数
 */
class Animal {
    name: string;
    voice: string;
    age: number;

    //constructor 构造函数,对象创建时调用
    constructor(props: { name: string, [propName: string]: any }) {
        //在实例方法中,this表示当前的实例
        this.name = props.name;
        this.age = props.age;
        this.voice = props.voice;
    }

    say(): string {
        return this.name + `(${this.age + '岁'}):` + this.voice;
    }
}

const dog = new Animal({name: '狗', age: 2, voice: '汪汪汪'});
const cat = new Animal({name: '猫', age: 1, voice: '喵喵喵'});
console.log(dog.say());
console.log(cat.say());

3.继承

/*
* 继承
* 使用继承后,子类将拥有父类所有的方法和属性
* 子类覆盖掉父类方法的形式,称为方法重写
 */
(function () {
    //父类
    class Animal {
        name: string;
        age: number;
        title: string;

        //constructor 构造函数,对象创建时调用
        constructor(props: { name: string, age: number, title: string }) {
            this.name = props.name;
            this.age = props.age;
            this.title = props.title;
        }

        say(): void {
            console.log(this.name + this.title);
        }
    }

    //子类
    class Dog extends Animal {
        color: string = "是白色的";
        
        run(): void {
            console.log(this.name + this.color);
        }
    }

    //子类
    class Cat extends Animal {
        color: string;

        //如果在子类中写了构造函数,在子类的构造函数中必须对父类的构造函数进行调用super();
        constructor(props: { name: string, age: number, title: string }) {
            super(props);
            this.color="是金色的";
        }

        test(): void {
            //在类的方法中 super实际表示当前类的父类
            super.say();
        }
    }

    const dog = new Dog({name: '小狗', age: 3, title: '汪汪汪~'});
    const cat = new Cat({name: '小猫', age: 2, title: '喵喵喵~'});
    cat.test();
    // dog.run();
})();

4.抽象类

/*
* 抽象类
* 以 abstract 开头的是抽象类
* 抽象类不能用来创建对象
* 专门用来继承的类
* 抽象类中可以添加抽象方法
 */
(function () {
    //父类
    abstract class Animal {
        name: string;
        age: number;
        title: string;

        //constructor 构造函数,对象创建时调用
        constructor(props: { name: string, age: number, title: string }) {
            this.name = props.name;
            this.age = props.age;
            this.title = props.title;
        }

        say(): void {
            console.log(this.name + this.title);
        }

        //定义一个抽象方法
        //抽象方法以abstract开头,并且没有方法体
        // 抽象方法只能定义在抽象类中,并且子类必须得对抽象方法进行重写
        abstract make(): void;
    }

    //子类
    class Cat extends Animal {
        color: string;

        //如果在子类中写了构造函数,在子类的构造函数中必须对父类的构造函数进行调用super();
        constructor(props: { name: string, age: number, title: string }) {
            super(props);
            this.color = "是金色的";
        }

        test(): void {
            //在类的方法中 super实际表示当前类的父类
            super.say();
        }

        //对父类抽象方法进行重写
        make(): void {
            console.log("干饭");
        }
    }

    const cat = new Cat({name: '小猫', age: 2, title: '喵喵喵~'});
    cat.test();
})();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值