TypeScript => 类

TypeScript笔记

5. TypeScript => 类
// 1. ts如何定义类
/*
class Person {
    name:string;
    constructor(name:string){
        this.name = name;
    }
    getName():string{
        return this.name;
    }
    setName(name:string):void {
        this.name = name;
    }
}
*/
// let per = new Person('李丹');
// console.log(per.getName());
// per.setName('王莉');
// console.log(per.getName());

// 2. 类里面的修饰符 ts 定义属性时 提供了三种修饰符
/* 
    pulic       :公有类型   => 在类里面、子类里面、类外面都可以访问 
    protected   :保护类型   => 在类里面、子类里面可以访问、 类外面不可以访问 
    private     :私有类型   => 在类里面可以访问、 子类里面和类外面不可以访问 
*/
/* 
class Person {
    public name:string;
    protected job:string;
    private age:number = 20;
    constructor(name:string,job:string){
        this.name = name;
        this.job = job;
    }
    getName():string{
        return this.name;
    }
    setName(name:string):void {
        this.name = name;
    }
}

class Child extends Person{
    constructor(name:string,job:string){
        super(name,job)
        console.log(this.job)
        // console.log(this.age)
    }
}
let c = new Child('丽丽','web');
*/

// 3. 静态方法
/*
class Person {
    static job:string ='web';
    name:string;
    constructor(name: string  = 'lili') {
        this.name = name;
    }
    //静态方法 无法直接访问类里面的属性 只能访问static 关键字的属性
    static getName(): string {
        return this.job;
    }
    //实例方法
    getName():string{
        return this.name;
    }
    setName(name:string):void{
        this.name = name;
    }
}
*/

// 4. ts中如何继承类  继承  extends spuer 同es6 
/*
class Person {
    name:string;
    constructor(name:string){
        this.name = name;
    }
    getName():string{
        return this.name;
    }
    setName(name:string):void {
        this.name = name;
    }
}

class Child extends Person{
    constructor(name:string){
        super(name)
    }
}
let c = new Child('丽丽')
console.log(c.getName())
c.setName('王朝马汉')
console.log(c.getName())
*/

// 5.多态
/*
class Animal {
    protected name: string;
    protected food: string;
    constructor(name: string, food: string) {
        this.name = name;
        this.food = food;
    }
    eat(): string {
        console.log(`${this.name}调eat方法`)
        return this.name
    }
}
class Dog extends Animal {
    constructor(name: string, food: string) {
        super(name, food);
    }
    // eat(){
    //     console.log(this.food);
    //     return this.name+'吃肉';
    // }
}
class Cat extends Animal {
    constructor(name: string, food: string) {
        super(name, food);
        this.food = food;
    }
    eat(): string {
        console.log(this.food);
        return this.name + '吃鱼';
    }
}
let dog = new Dog('山治', '肉')
let cat = new Cat('汤姆', '鱼')
console.log(dog.eat())
console.log(cat.eat())
*/

// 6. 抽象类
// typescript 中的抽象类:提供给其他类继承的基类, 不能被实例化。
// 使用 abstract 关键字定义抽象类 和 抽象方法,抽象类中的抽象方法 不包含具体实现 并且 必须在派生类(子类)中实现。
//      abstract 抽象方法只能放在抽象类中 它的子类必须包含抽象方法
/*
abstract class Model {
    public name:string;
    constructor(name:string){
        this.name = name;

    }
    //定义抽象方法 不包含具体实现 
    abstract eat():any;
}
class Dog extends Model {
    constructor(name:string){
        super(name);
    }
    //子类 必须存在抽象类中的抽象方法 必须在派生类(子类)中实现。
    eat(){
        console.log(`${this.name} is eat` )
    }
}
let dog = new Dog('dog');
dog.eat();
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值