Vue3+TypeScript从入门到精通系列之:抽象类

一、TypeScript抽象类

(() => {

//抽象类:包含抽象方法,也可以包含实例方法
//抽象类不能被实例化,让子类进行实例化嗯及实现

//定义一个抽象类
abstract class Animal{
//抽象属性
abstract name: string
//抽象方法
abstract eat()

//实例方法
say(){
console.log(this.name,"都是动物")
}

}

class Dog extends Animal{
name: string = "小狗"
eat(){
  console.log(this.name,"喜欢吃骨头")
}
}

//实例化Dog对象
const dog:Dog = new Dog()
dog.eat()
dog.say()


})()

二、TypeScript抽象类代码转化为js代码

tsc ./抽象类.ts

抽象类.js代码如下所示:

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
(function () {
    //抽象类:包含抽象方法,也可以包含实例方法
    //抽象类不能被实例化,让子类进行实例化嗯及实现
    //定义一个抽象类
    var Animal = /** @class */ (function () {
        function Animal() {
        }
        //实例方法
        Animal.prototype.say = function () {
            console.log(this.name, "都是动物");
        };
        return Animal;
    }());
    var Dog = /** @class */ (function (_super) {
        __extends(Dog, _super);
        function Dog() {
            var _this = _super !== null && _super.apply(this, arguments) || this;
            _this.name = "小狗";
            return _this;
        }
        Dog.prototype.eat = function () {
            console.log(this.name, "喜欢吃骨头");
        };
        return Dog;
    }(Animal));
    //实例化Dog对象
    var dog = new Dog();
    dog.eat();
    dog.say();
})();

三、js输出如下所示

node ./抽象类.js
小狗 喜欢吃骨头
小狗 都是动物
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最笨的羊羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值