TypeScript前端学习(四)

前言

还是分享的笔记,大佬请绕行!


一、类的定义和继承



typescript 复制代码
console.log("----对象---");
var people = {
    "name": "张三",
    syaHi() {
        console.log("HI,my name is " + this.name);
    }
}
people.syaHi();

console.log("----类---");
class People {
    name: string;

    constructor(name: string) {
        this.name = name;
    }

    syaHi() {
        console.log("HI,my name is " + this.name);
    }
}

//实例化类
var people2 = new People("李四");
people2.syaHi();

console.log("----类的继承---");
//类的继承
class Student extends People {
    constructor(name: string) {
        super(name);
    }

    syaHi() {
        console.log("HI,my name is " + this.name);
    }

    makeWorks() {
        console.log("我在做作业");
    }
}

var student = new Student("小明");
student.syaHi() + "," + student.makeWorks();

class Teacher extends People {
    subject: string;

    constructor(name: string, subject: string) {
        super(name);
        this.subject = subject;
    }

    syaHi() {
        super.syaHi();
        console.log("同学们好");
    }

    teach() {
        console.log(this.name + ",我在教" + this.subject);
    }
}

var teacher = new Teacher("李老师", "数学");
teacher.syaHi();
console.log("----------")
teacher.teach();

示例效果

二、类的访问权限




typescript 复制代码
console.log("----类的访问权限---");
class Animal {
    //private className:string="Animal";  //私有属性,只能在类内部访问(继承他的类用就会报错了)
    className: string = "Animal";

    protected constructor() {
        this.className = "Animal";    //受保护的构造函数,只能在类内部访问(继承他的类用就会报错了)
    }
}
//var animal = new Animal();  //TS2674: Constructor of class Animal is protected and only accessible within the class declaration.

class Dog extends Animal {
    name: string;

    constructor(name: string) {
        super();
        this.name = name;
    }

    run() {
        console.log(this.name + "在跑");
    }
}
var dog = new Dog("小狗");
dog.run();

示例运行效果

注释的语句,报错描述写在注释里了

报错就是因为设置了protected等关键字

三、只读属性与存取器



typescript 复制代码
console.log("----只读属性---")

//
class Circle {
    readonly PI: number = 3.14;
    readonly r: number;

    constructor(r: number) {
        this.r = r;
    }

    getArea() {
        return this.PI * this.r * this.r;
    }
}
var circle = new Circle(10);
console.log(circle.getArea());

//circle.r = 20; //TS2540: Cannot assign to 'r' because it is a read-only property.

console.log("----存取器---");
class Circle2 {
    readonly PI: number = 3.14;
    protected r: number = 0;
    set R(r: number) {
        if (r < 0) {
            return;
        }
        this.r = r;
    }
    get R(): number {
        return this.r;
    }
    getArea() {
        return this.PI * this.r * this.r;
    }
}
var circle2 = new Circle2();
circle2.R = 10;
console.log(circle2.R);
console.log(circle2.getArea());

示例运行效果


总结

  • 看起来跟其他面向对象的后端语言真像
  • 这次没有用tsc命令生成js对比,有兴趣的可以自己生成对比看看
    纯粹的笔记备忘,uping!
相关推荐
光影少年16 分钟前
vue中$set原理
前端·javascript·vue.js
codecodegirl24 分钟前
实现在h5中添加日历提醒:safari唤起系统日历,其它浏览器跳转google日历
前端·javascript·vue.js·html5
Attacking-Coder30 分钟前
前端面试宝典---webpack原理解析,并有简化版源码
前端·面试·webpack
wuhen_n42 分钟前
Canvas特效实例:黑客帝国-字母矩阵(字母雨)
前端·javascript·矩阵·html5·canvas·canva可画
ʚʕ̯•͡˔•̯᷅ʔɞ LeeKuma44 分钟前
SSR vs SSG:前端渲染模式终极对决(附 Next.js/Nuxt.js 实战案例)
开发语言·前端·javascript
前后端杂货铺1 小时前
uniapp+vue3+ts 使用canvas实现安卓端、ios端及微信小程序端二维码生成及下载
android·前端·ios·微信小程序·uni-app·canavas·二维码海报生成
玄晓乌屋2 小时前
nvm for windows 安装低版本 node 丢失 npm 安装
前端·npm·node.js
SparklingTheo2 小时前
npm init、换源问题踩坑
前端·npm·node.js
F2E_Zhangmo3 小时前
webpack5启动项目报错:process is not defined
前端·vue.js·webpack·webpack5
万物得其道者成3 小时前
使用 Vue3 + Webpack 和 Vue3 + Vite 实现微前端架构(基于 Qiankun)
前端·webpack·架构