资讯专栏INFORMATION COLUMN

TypeScript--类

RobinQu / 532人阅读

摘要:中定义类属性前面省略了关键词构造函数实例化类的时候触发的方法继承在运动在运动子类在工作修饰符公有在当前类里面子类类外面都可以访问保护类型在当前类里面子类里面可以访问,在类外部没法访问私有在当前类里面可以访问,子类类外部都没法访问在运动在

ts中定义类
class Person{
    name:string;  //属性  前面省略了public关键词
    constructor(n:string){  //构造函数   实例化类的时候触发的方法
        this.name = n;
    }
    run():void{
        alert(this.name);
    }
}
var p = new Person("lisi");
p.run()

class Person{
    name:string;
    constructor(name:string){
        this.name = name;
    }
    getName():string{
        return this.name;
    }
    setName(name:string):void{
        this.name = name;
    }
}
var p = new Person("lisi");
alert(p.getName());
p.setName("wanwu");
alert(p.getName());

继承
class Person{
    name:string;
    constructor(name:string){
        this.name = name;
    }
    run():string{
        return `${this.name}在运动`;
    }
}
var p = new Person("lisi");
alert(p.run());

class Web extends Person{
    constructor(name:string){
        super(name);
    }
    run():string{
        return `${this.name}在运动--子类`
    }
    work(){
        alert(`${this.name}在工作`)
    }
}

var w = new Web("wanwu");
alert(w.run());
w.work();

修饰符

public :公有 在当前类里面、 子类 、类外面都可以访问

protected:保护类型 在当前类里面、子类里面可以访问 ,在类外部没法访问

private :私有 在当前类里面可以访问,子类、类外部都没法访问

public
class Person{
    public name:string;
    constructor(name:string){
        this.name = name;
    }
    run():string{
        return `${this.name}在运动`;
    }
}
var p = new Person("lisi");
alert(p.run());

class Web extends Person{
    constructor(name:string){
        super(name);
    }
    run():string{
        return `${this.name}在运动--子类`
    }
    work(){
        alert(`${this.name}在工作`)
    }
}
var w = new Web("wanwu");
alert(w.run());

protected
class Person{
    protected name:string;
    constructor(name:string){
        this.name = name;
    }
    run():string{
        return `${this.name}在运动`;
    }
}
var p = new Person("lisi");
alert(p.run());

class Web extends Person{
    constructor(name:string){
        super(name);
    }
    run():string{
        return `${this.name}在运动--子类`
    }
    work(){
        alert(`${this.name}在工作`)
    }
}
var w = new Web("wanwu");
alert(w.run());

静态属性 静态方法
class Person{
    public name:string;  /*公有属性*/
    static sex = "男" //静态属性
    constructor(name:string){
        this.name=name;
    }
    run():string{
        return `${this.name}在运动`
    }

    static print(){
        // alert("print方法"+this.name);
        alert("print方法"+Person.sex);
    }
}

var p = new Person("lisi");
Person.print();

多态

多态:父类定义一个方法不去实现,让继承它的子类去实现,每一个子类有不同的表现
多态属性继承

父类有方法,子类也有同样的方法
class Animal{
    name:string;
    constructor(name:string){
        this.name = name;
    }
    //具体吃什么,不知道,具体吃什么,让继承它的子类去实现,每一个子类的表现不一样
    eat(){
        console.log("吃的方法")
    }
}

class Dog extends Animal{
    constructor(name:string){
        super(name);
    }
    eat(){
        return this.name +"吃骨头"
    }
}
var d = new Dog("d");
alert(d.eat());

class Cat extends Animal{
    constructor(name:string){
        super(name);
    }
    eat(){
        return this.name +"吃鱼"
    }
}
var c = new Cat("c");
alert(c.eat());

父类有方法,子类没有同样的方法
class Animal{
    name:string;
    constructor(name:string){
        this.name = name;
    }
    //具体吃什么,不知道,具体吃什么,让继承它的子类去实现,每一个子类的表现不一样
    eat(){
        console.log("吃的方法")
    }
}

class Dog extends Animal{
    constructor(name:string){
        super(name);
    }
    eat(){
        return this.name +"吃骨头"
    }
}
var d = new Dog("d");
alert(d.eat());

class Cat extends Animal{
    constructor(name:string){
        super(name);
    }
    // eat(){
    //     return this.name +"吃鱼"
    // }
}
var c = new Cat("c");
alert(c.eat());

抽象类

抽象类:它是提供其它继承的基类,不能直接被实例化

abstract关键字定义抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类中实现

abstract抽象方法只能放在抽象类里面

抽象类和抽象方法用来定义标准,标准:Animal这个类要求它的子类必须包含相同的方法,如eat()

直接被实例化,报错
abstract class Animal{
    public name:string;
    constructor(name:string){
        this.name = name;
    }
    abstract eat():any;
    run(){
        console.log("运动")
    }
}

var a = new Animal();

父类定义的抽象方法,子类没有定义,报错
abstract class Animal{
    public name:string;
    constructor(name:string){
        this.name = name;
    }
    abstract eat():any;
    run(){
        console.log("运动")
    }
}

class Dog extends Animal{
    constructor(name:string){
        super(name);
    }
    // eat(){
    //     console.log(this.name+"吃骨头")
    // }

}

正常

类非抽象方法,子类可以不写

abstract class Animal{
    public name:string;
    constructor(name:string){
        this.name = name;
    }
    abstract eat():any;
    run(){
        console.log("运动")
    }
}


class Dog extends Animal{
    constructor(name:string){
        super(name);
    }
    eat(){
        console.log(this.name+"吃骨头")
    }

}

var d = new Dog("d");
d.eat()

class Cat extends Animal{
    constructor(name:string){
        super(name);
    }
    eat(){
        console.log(this.name+"吃鱼")
    }
}

var c = new Cat("c");
c.eat()

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/101691.html

相关文章

  • 一篇文章带你过一遍 TypeScript

    摘要:泛型通过在函数接口类变量名后使用定义。抽象类可以包括具体实现一个类只能继承一个类,但是可以实现多个接口。该开源项目目前由社区进行维护。通常通过中的字段,或者声明文件进行声明。 TypeScript 是 Javascript 的一个超集,提高了代码的可读性和可维护性。Typescript 官网提供的文档已经相当完善,但完整地看一遍需要一定的时间,本文试将 TypeScript 中要点提出...

    AlphaWallet 评论0 收藏0
  • 【译】使用TypeScript两年后-值得吗?

    摘要:弄了一个持续更新的笔记,可以去看看,链接地址此篇文章的地址使用两年后值得吗基础笔记的地址可以也可以。使用,你可以使用抽象类等功能。有关抽象类的更多信息支持,和方法,只读属性。 弄了一个持续更新的github笔记,可以去看看,链接地址:Front-End-Basics 此篇文章的地址:使用TypeScript两年后-值得吗? 基础笔记的github地址:https://githu...

    RyanQ 评论0 收藏0
  • TypeScript VS JavaScript 深度对比

    摘要:是事件驱动的,只根据用户的操作做出相应的反应处理。中的数据要求带有明确的类型,不要求。这些小小的变化可能会产生严重的意想不到的后果,因此有必要撤销这些变化。的优势相比于,也有一些明显优势。因此在应对大型开发项目时,使用更加合适。 showImg(https://segmentfault.com/img/bV1Dx7?w=740&h=322); TypeScript 和 JavaScri...

    William_Sang 评论0 收藏0
  • 从 JavaScript 到 TypeScript - 声明

    摘要:要为变量或者常量指定类型也很简单,就是在变量常量名后面加个冒号,再指定类型即可,比如声明函数是类型,即返回值是类型声明参数是类型声明是无返回值的声明是这段代码演示了对函数类型参数类型和变量类型地声明。变量函数参数和返回值需要申明类型。 从 JavaScript 语法改写为 TypeScript 语法,有两个关键点,一点是类成员变量(Field)需要声明,另一点是要为各种东西(变量、参数...

    Flands 评论0 收藏0
  • TypeScript快速入门

    摘要:添加了可选的静态类型注意并不是强类型和基于类的面向对象编程。类类型接口示例接口更注重功能的设计,抽象类更注重结构内容的体现模块中引入了模块的概念,在中也支持模块的使用。 一:Typescript简介 维基百科: TypeScript是一种由微软开发的自由和开源的编程语言。它是JavaScript的一个严格超集,并添加了可选的静态类型和基于类的面向对象编程。C#的首席架构师以及Delp...

    moven_j 评论0 收藏0
  • TypeScript入门学习之路

    摘要:学习之路学习之路安装环境起步开发工具自动编译文件中的数据类型中的函数中类的定义继承中的继承中的继承类的静态属性和静态方法类的多态的抽象类中的接口中的泛型学习之路安装环境查看版本起步新建通过命令编译此时路径下 ...

    jemygraw 评论0 收藏0

发表评论

0条评论

RobinQu

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<