typescript - 4.es5与typescript的类与继承

ES5中的类与类的继承

(1)简单的类

function Person() {

        this.name = '张三';
        this.age = 20;
    }
    var p = new Person();
    alert(p.name);

(2)构造函数和原型链里面增长方法

function Person() {

        this.name = '张三';  /*属性*/
        this.age = 20;
        this.run = function () {
            alert(this.name + '在运动');
        }

    }
    //原型链上面的属性会被多个实例共享   构造函数不会
    Person.prototype.sex = "男";
    Person.prototype.work = function () {
        alert(this.name + '在工做');

    }
    var p = new Person();
    // alert(p.name);
    // p.run();
    p.work();

(3)ES5静态方法

function Person() {
    this.name = '张三';  /*属性*/
    this.age = 20;
    this.run = function () {  /*实例方法*/
        alert(this.name + '在运动');
    }
}

Person.getInfo = function () {
    alert('我是静态方法');
}

//调用静态方法
Person.getInfo();

(4)es5里面的继承 对象冒充实现继承

对象冒充能够继承构造函数里面的属性和方法 可是无法继承原型链上面的属性和方法
``` javascript
function Person() {
this.name = '张三'; /属性/
this.age = 20;
this.run = function () { /实例方法/
alert(this.name + '在运动');
}javascript

}
Person.prototype.sex = "男";
Person.prototype.work = function () {
    alert(this.name + '在工做');

}

//Web类 继承Person类   原型链+对象冒充的组合继承模式

function Web() {

    Person.call(this);    /*对象冒充实现继承*/
}

var w = new Web();
// w.run();  //对象冒充能够继承构造函数里面的属性和方法

w.work();  //对象冒充能够继承构造函数里面的属性和方法   可是无法继承原型链上面的属性和方法

```java

(5)es5里面的继承 原型链实现继承

原型链实现继承:能够继承构造函数里面的属性和方法 也能够继承原型链上面的属性和方法typescript

function Person() {
            this.name = '张三';  /*属性*/
            this.age = 20;
            this.run = function () {  /*实例方法*/
                alert(this.name + '在运动');
            }

        }
        Person.prototype.sex = "男";
        Person.prototype.work = function () {
            alert(this.name + '在工做');

        }

        //Web类 继承Person类   原型链+对象冒充的组合继承模式
        function Web() {

        }

        Web.prototype = new Person();   //原型链实现继承
        var w = new Web();
        //原型链实现继承:能够继承构造函数里面的属性和方法 也能够继承原型链上面的属性和方法
        //w.run();

        w.work();

(6)原型链继承的问题?有参数的状况

实例化子类的时候无法给父类传参函数

function Person(name,age){
            this.name=name;  /*属性*/
            this.age=age;
            this.run=function(){  /*实例方法*/
                alert(this.name+'在运动');
            }

    }      
    Person.prototype.sex="男";
    Person.prototype.work=function(){
            alert(this.name+'在工做');

    }
       
      
    function Web(name,age){

        
    }

    Web.prototype=new Person();

    var w=new Web('赵四',20);   //实例化子类的时候无法给父类传参

    w.run();

    // var w1=new Web('王五',22);

(7)原型链+对象冒充的组合继承模式

有参数的的状况下,原型链+对象冒充,能够传参给子类ui

function Person(name,age){
            this.name=name;  /*属性*/
            this.age=age;
            this.run=function(){  /*实例方法*/
                alert(this.name+'在运动');
            }

    }      
    Person.prototype.sex="男";
    Person.prototype.work=function(){
            alert(this.name+'在工做');

    }
       
      
    function Web(name,age){

        Person.call(this,name,age);   //对象冒充继承   实例化子类能够给父类传参
    }

    Web.prototype=new Person();

    var w=new Web('赵四',20);   //实例化子类的时候无法给父类传参

    // w.run();
    w.work();

    // var w1=new Web('王五',22);

(8)原型链+对象冒充继承的另外一种方式

Web.prototype=new Person();的另外一种写法this

Web.prototype=Person.prototype;

Typescript中的类与继承

(1)ts中类定义

class Person{

                name:string;   //属性  前面省略了public关键词

                constructor(n:string){  //构造函数   实例化类的时候触发的方法
                    this.name=n;
                }

                run():void{

                    alert(this.name);
                }

            }
            var p=new Person('张三');

            p.run()

(2)ts中的继承:extends与 super

class Web extends Person{
        constructor(name:string){

            super(name);  /*初始化父类的构造函数*/
        }
    }

    var w=new Web('李四');
    alert(w.run());

(3)类的修饰符

public

在当前类里面、 子类 、类外面均可以访问es5

protected:保护类型

在当前类里面、子类里面能够访问 ,在类外部无法访问prototype

private :私有

在当前类里面能够访问,子类、类外部都无法访问code

(4)静态属性与方法 static

class Per{
        public name:string;
        public age:number=20;
        //静态属性

        static sex="男";
        constructor(name:string) {
                this.name=name;
        }
        run(){  /*实例方法*/

            alert(`${this.name}在运动`)
        }
        static print(){  /*静态方法  里面无法直接调用类里面的属性*/

            alert('print方法'+Per.sex);
        }
    }

(5)readonly修饰符

class Octopus {
    readonly name: string;
    readonly numberOfLegs: number = 8;
    constructor (theName: string) {
        this.name = theName;
    }
}
let dad = new Octopus("Man with the 8 strong legs");
dad.name = "Man with the 3-piece suit"; // 错误! name 是只读的.

(6)抽象类 abstract

抽象类作为其它派生类的基类使用。 它们通常不会直接被实例化。 不一样于接口,抽象类能够包含成员的实现细节。 abstract关键字是用于定义抽象类和在抽象类内部定义抽象方法。对象

abstract class Animal{
    
    public name:string;
    constructor(name:string){

        this.name=name;

    }
    abstract eat():any;  //抽象方法不包含具体实现而且必须在派生类中实现。
    
    run(){

        console.log('其余方法能够不实现')
    }
}


// var a=new Animal() /*错误的写法*/
 


class Dog extends Animal{

    //抽象类的子类必须实现抽象类里面的抽象方法
    constructor(name:any){
        super(name)
    }
    eat(){

        console.log(this.name+'吃粮食')
    }
}

var d=new Dog('小花花');
d.eat();




class Cat extends Animal{

    //抽象类的子类必须实现抽象类里面的抽象方法
    constructor(name:any){
        super(name)
    }
    run(){


    }
    eat(){

        console.log(this.name+'吃老鼠')
    }
    
}

var c=new Cat('小花猫');
c.eat();
相关文章
相关标签/搜索