三、類

官網文檔地址 http://www.typescriptlang.org/docs/index.html

es5中類使用

es5中類使用

function Person(){
	
	//屬性
	this.name = 'name';
	this.age = 22;
	this.run = function(){
		// 方法【爲實例方法,通過new對象實例使用】
	}
}
====================
//原型類Person通過prototype增加類的屬性【會被多個實例共享,而內部構造的不會共享】及方法
Person.prototype.sex = '男';// 靜態屬性 
Person.prototype.work = function(){
	// 方法
}
====================
//靜態方法設置
//Person.prototype.sex = '男';// 靜態屬性 
// 原型類.方法
Person.getInfo =  function(){
	// 靜態方法
	alert('這是靜態方法')
}

var pers = new Person();
console.log(pers.name);
console.log(pers.run());
console.log(pers.getInfo())
====================
// web繼承Person實現,原型鏈+對象冒充的組合繼承模式
function Web(){
	Person.cal(this);/*對象冒充實現繼承*/
}
var wb = new Web();
wb.run() // 對象冒充可以繼承構造函數裏面的屬性和方法
wb.work()// 無法執行,對象冒充不能繼承原型類的通過prototype添加的方法屬性。
====================
//原型鏈 實現繼承
Web.prototype = new Person();// 原型鏈實現繼承
var w = new Web();
w.run();// 原型鏈實現繼承可以繼承構造函數裏面的屬性和方法也可以繼承原型鏈上面的屬性和方法
====================
原型鏈繼承存在的問題:實例化子類時無法給父類傳參。
function Web(name,age){}
Web.prototype = new Person();
var w = new Web("xiaoxi",22);// 這時子類的name,age 無法傳遞給父類 Person 進行初始化父類的name和age。
==================== 
以上兩種方式相互結合 優劣互補=> 原型鏈+對象冒充的組合繼承模式
function Web(name,age){
	Person.call(this,name,age);//對象冒充
}
Web.prototype = new Person(); 或 Web.prototype =Person.prototype;
var w = new Web("xiaoxi",22);// 原型鏈


typescript中類使用

typescript中類使用
s2.7引入了一個新的flag:--strictPropertyInitialization。這個flag檢查並確保一個類在初始化時,每一個屬性都必須在構造器內初始化,或者在屬性定義時賦予初始值。
 class person{ name!: string;}   // definite assignment assertion 明確的賦值斷言,由用戶自己保證,這個屬性在使用前會初始化,類型檢查不管這個屬性
定義
class Person{
	public name:string;// 屬性 訪問修飾符 public protected private
	constructor(name:string){// 構造函數 實例化類時觸發
		this.name=name;
	}
	run():void{
		// 方法
	}
	getName():string{
		return this.name;
	}
	setName(name:string):void{
		this.name=name;
	}
}

var p = new Person("name")
====================
繼承 extends、super,父類和子類有相同的方法時調用方法時會先在子類中查找,在再父類中查找
class Web extends Person{
	constructor(name:string){
		super(name);// 初始化父類構造函數
	}
	run():void{
	
	}
}
====================
訪問修飾符 public protected private
public 範圍 在類內部 子類內部 類外面都可以訪問
protected 範圍 在類內部 子類內部 都可以訪問
private 範圍 在類內部 都可以訪問

class Person{
	public name:string;
	constructor(name:string){
		this.name = name;
	}
	protected run():void{
        console.log('22')
		//return `${this.name} 類內部`;// 類內部訪問
    }
}
// let p = new Person('xiaoxi');

// console.log(p.name) // 類外面訪問
// p.run();// 類外面訪問
// console.log(p.work());// 類外面訪問

class Web extends Person{
    constructor(name:string){
        super(name);
    }
    work():void{
        this.run();// 子類內部訪問父類
        console.log(`${this.name}dd`)//子類內部訪問父類
    }
}
let w = new Web('xiaoxi2');
// console.log(w.name) // 子類外面訪問
// w.run();// 類外面訪問
console.log(w.work());// 類外面訪問
====================
靜態屬性 靜態方法 ,關鍵字 static
靜態屬性及方法原因:爲了不進行實例化而直接使用方法。Person.method

class Person{
	public name:string;
	static age:number=2;// 靜態屬性
	constructor(name:string){
		this.name = name;
	}
	public run():void{
        console.log('22')
		//return `${this.name}類內部`;// 類內部訪問
    }
    static printWord(){// 靜態方法
    	console.log('word${this.age}')// 注意靜態方法中無法直接調用類裏面的非靜態屬性
    }
}

多態:父類定義一個方法不去實現,讓繼承它的子類去實現 每一個子類有不同的表現
1 接口 和 實現接口並覆蓋接口中同一方法的幾不同的類體現的
2 父類 和 繼承父類並覆蓋父類中同一方法的幾個不同子類實現的.
java裏沒有多繼承,一個類之能有一個父類。而繼承的表現就是多態。一個父類可以有多個子類,而在子類裏可以重寫父類的方法(例如方法print()),這樣每個子類裏重寫的代碼不一樣,自然表現形式就不一樣。這樣用父類的變量去引用不同的子類,在調用這個相同的方法print()的時候得到的結果和表現形式就不一樣了,這就是多態,相同的消息(也就是調用相同的方法)會有不同的結果。
class Animal{
    name:string;
    constructor(name:string){
        this.name=name;
    }
    eat(){}
}
class Dog extends Animal{
    constructor(name:string){
        super(name)
    }
    eat():void{
        console.log(`${this.name}啃骨頭`)
    }
}
class Cat extends Animal{
    constructor(name:string){
        super(name)
    }
    eat():void{
        console.log(`${this.name}喫魚`)
    }
}

抽象類及抽象方法
用abstract關鍵字定義抽象類和抽象方法,抽象類中的抽象方法不包含具體實現並且必須在派生類中實現。必須包含抽象方法。abstract抽象方法只能放在抽象類裏面,抽象類不能創建實例
abstract class Animal{
    abstract eat():any;
    run(){
    	console.log('run')
    }
}
class Dog extends Animal{
    constructor(){
        super()
    }
    eat(){
        console.log('eat fish');
    }
}
class Cat extends Animal{
    constructor(){
        super()
    }
    eat(){
        console.log('eat fish2');
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章