Typescript 入门(三)-类

一、在ES5中类定义和继承 

1、定义类
function Peson(name,age){
 this.name = name;
 this.age = age;

Person.prototype.display = function(){
 console.log(this.name + '\t'+ this.age)
}
var p = new Person();

2、静态方法
Person.disp = function(){
    console.log('我是静态方法')
}

3、继承
function Man(name,age){
    Person.call(this,[name,age]);
    this.sex = 1;
}
Man.prototype = Person.prototype ;
var man = new Man('zzz',12);
man.display();

二、在TS中类定义和继承 

1、类定义
class Animal {
    //定义属性
    public name:string;
    // 构造函数
    constructor(name:string){
        this.name = name;
    }
   // 方法
    display():void{
        console.log(this.name);
    }
}
var animal = new Animal("cat");

2、继承 
class Cat extends Animal {
    constructor(name:string){
        super(name);
    }
}

3、类的修饰符
public /protected/private
public  在当前类、子类、类外部都可以访问
protected  在当前类、子类可以访问,类外部不能访问
private 在当前类可以访问,子类和类外部不能访问
如果不写是公有的属性

4、静态属性和静态方法
class Dog extends Animal {
    private age:number;
    // 静态属性
    private static count:number = 1;
    constructor(name:string,age:number){
        super(name);
        this.age = age;
    }
    // 静态方法
    static print():void {
        console.log("print" + Dog.count)
    }
}
Dog.print();

5、多态
class Animal {
    private name:string;
    constructor(name:string){
        this.name = name;
    }
    public eat():void{
        console.log("eat");
    }
}
// 具体的子类实现eat方法
class Dog extends Animal {
    constructor(name:string){
        super(name);
    }
    public eat() : void {
    }
}

6、抽象类、抽象方法
abstract class Animal {
    private name:string;
    constructor(name:string){
        this.name = name;
    }
    public abstract eat():void;
}
class Dog extends Animal {
    constructor(name:string){
        super(name);
    }
    public eat():void{
    }
}

一、在ES5中类定义和继承 

1、定义类
function Peson(name,age){
 this.name = name;
 this.age = age;

Person.prototype.display = function(){
 console.log(this.name + '\t'+ this.age)
}
var p = new Person();

2、静态方法
Person.disp = function(){
    console.log('我是静态方法')
}

3、继承
function Man(name,age){
    Person.call(this,[name,age]);
    this.sex = 1;
}
Man.prototype = Person.prototype ;
var man = new Man('zzz',12);
man.display();

二、在TS中类定义和继承 

1、类定义
class Animal {
    //定义属性
    public name:string;
    // 构造函数
    constructor(name:string){
        this.name = name;
    }
   // 方法
    display():void{
        console.log(this.name);
    }
}
var animal = new Animal("cat");

2、继承 
class Cat extends Animal {
    constructor(name:string){
        super(name);
    }
}

3、类的修饰符
public /protected/private
public  在当前类、子类、类外部都可以访问
protected  在当前类、子类可以访问,类外部不能访问
private 在当前类可以访问,子类和类外部不能访问
如果不写是公有的属性

4、静态属性和静态方法
class Dog extends Animal {
    private age:number;
    // 静态属性
    private static count:number = 1;
    constructor(name:string,age:number){
        super(name);
        this.age = age;
    }
    // 静态方法
    static print():void {
        console.log("print" + Dog.count)
    }
}
Dog.print();

5、多态
class Animal {
    private name:string;
    constructor(name:string){
        this.name = name;
    }
    public eat():void{
        console.log("eat");
    }
}
// 具体的子类实现eat方法
class Dog extends Animal {
    constructor(name:string){
        super(name);
    }
    public eat() : void {
    }
}

6、抽象类、抽象方法
abstract class Animal {
    private name:string;
    constructor(name:string){
        this.name = name;
    }
    public abstract eat():void;
}
class Dog extends Animal {
    constructor(name:string){
        super(name);
    }
    public eat():void{
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值