typescript中的类

继承 & 重写

​ 概念跟java那些都很像(具体看下面的例子)

修饰符(默认缺省状态为:public)

  1. public

  2. protected : 在 该类和子类 都能调用,不能在类外(子类除外)调用

  3. private : 只能在 该类 里面调用;子类都调用不了

  4. readonly : 必须在声明或构造函数中初始化。

    class Animal {
    	name: string;
    	// 私有属性,只能在Animal类内部使用
    	private unique: string;
    	// 只读属性   
    	readonly readOnly: string = "readOnly";
    	// 受保护属性(能被继承)
    	protected species: string = '动物';
    	// 不能在类外(子类除外)调用,实例不了
    	// 受保护构造函数,能够在子类的构造函数中调用
    	protected constructor(theName: string, unique: string) {
    		this.name = theName;
    		this.unique = unique;
    	}
        move(distanceInMeters: number = 0) {
            console.log(`Animal moved ${distanceInMeters}`);
        }
        getReadOnly() {
        	return this.readOnly;
        }
        getName() {
    
        	console.log(`My name is ${this.name}`);
        }
    }
    
    // 继承
    class Dog extends Animal {
    	// Dog 也能继承基类的 受保护属性
    	protected species: string = '狗';
    	constructor(name: string) {
            // 运行父类的构造函数
    		super(name, '属于Animal私有属性');
    	}
    	bark() {
    		console.log('Woof! Woof!');
    	}
    	//重写
    	getName() {
    		console.log(`我是一条${this.species}`);
            // 运行父类的方法
    		super.getName();
    	}
    }
    
    // 虽然dog 的变量类型为Animal , 但它是一个Dog 实例
    let dog: Animal = new Dog("猪猪");
    dog.getName();  //我是一条狗        My name is 猪猪
    console.log(dog.getReadOnly());  // readOnly
    
    
    

其他常见的概念

存取器(set , get)

经常搭配private属性一起出现,主要是出于安全方面的考虑。如果只设置get,就相当于设置了readyonly 修饰符,不能再进行修改了。(ES5环境或以上才能使用)

静态属性

特点:该属性存在于类中,而非实例中

class Animal {
	// 私有属性,只能在Animal类内部使用
	private _unique: string;
	// 静态属性
	static _name: string;
	// 设置存取器
	get unique(): string{
		return this._unique;
	}
	set unique(unique: string) {
		this._unique = unique;
	}
	getInfo(): Object {
		return {
			unique: this._unique,
			// 静态属性的调用
			_name: Animal._name
		}; 
	}
}


let animal: Animal = new Animal();
animal.unique = "唯一";
console.dir(animal.getInfo());

抽象类(abstract)

特点:不能被直接实例,要通过继承。抽象类不同于接口,可以包含详细的实现过程;抽象方法与接口相似 (存在抽象方法的类一定是抽象类)

高级技巧

  1. 类当接口用

  2. 构造函数

    构造函数添加修饰符,相当于声明并初始化了该成员

abstract class Animal {
	// 类似于定义接口
    abstract printMeeting(): void;
    // 构造函数的参数设置了修饰符,相当于已经作了声明 省了
    // public name: string; 这一句声明
    constructor(public name: string) {

    }
    // 抽象类中也可以包含详细的实现过程
    printName(): void{
    	console.log('Animal name: '+ this.name);
    }
}

// 抽象类必须通过继承,不能直接实例。
class Dog extends Animal {
	constructor(name: string){
		super(name);
	}
	printMeeting(): void{
		console.log("汪汪汪~~");
	}
	printDog(): void{
		console.log("Dog 类所特有的");
	}

}


let dog: Dog = new Dog("哮天犬");
dog.printName();  //Animal name: 哮天犬
dog.printMeeting();   //汪汪汪~~
dog.printDog();

let dog1: Animal = new Dog("二哈");   //------ dog1 的类型引用是Animal  
dog1.printMeeting();
//dog1.printDog();    =====> 调用不了,因为Animal类没有这个方法   


//类也可以当成接口
class Point {
	x: number;
	y: number
}
interface Point3d extends Point {
	z: number;
}

let point3d: Point3d = {x: 34, y: 5675, z: 6543};


原文地址:https://www.cnblogs.com/zxn-114477/p/15043045.html