TS中的类
1.1 ES5中的类
function Person(){
this.name = 'kjh';
this.age= 20;
}
var newPerson = new Person();
console.log(newPerson)
function Person(){
this.name = 'kjh';
this.age= 20;
this.sayHi = function(){
console.log('hi')
}
}
Person.prototype.job = 'teacher'
var newPerson = new Person();
console.log(newPerson)
console.log(newPerson.sayHi());
function Person(){
this.name = 'kjh';
this.age= 20;
}
Person.qq = function(){
console.log('861918672')
}
Person.qq();
function Person(){
this.name = 'kjh';
this.age= 20;
}
function Son(){
Person.call(this)
}
var newSon = new Son();
console.log(newSon.name)
console.log(newSon.age)
2.1 TS中的类
class Person {
name:string;
constructor(name:string){
this.name = name;
}
run():void{
console.log(this.name);
}
getName():string{
return this.name;
}
setName(name:string):void{
this.name = name;
}
}
var p1 = new Person('康家豪');
p1.run();
console.log(p1.getName());
p1.setName('李四')
console.log(p1.getName());
- typeSctipt 中实现继承 extends 、super
class Person {
name:string;
constructor(name:string){
this.name = name;
}
run():string{
return `${
this.name} 在运动!`
}
}
var p = new Person('张三');
console.log(p.run());
class Son extends Person{
constructor(name:string){
super(name);
}
}
var s = new Son('十分');
console.log(s.name)
- 类里面的修饰符 ts在定义属性的时候给我们提供了三种修饰符
class Person {
name:string;
age:number;
constructor(name:string,age:number){
this.name = name;
this.age = age;
}
sayHi():string{
return `${
this.name}的年龄是${
this.age} ---父类(当前类)`
}
}
class Son extends Person{
constructor(name:string,age:number){
super(name,age)
}
talk():string{
return `${
this.name}的年龄是${
this.age}---子类`
}
}
var s1 = new Son('张三',99)
console.log(s1.talk())
console.log(s1.sayHi());
var p1 = new Person('康家豪',21);
console.log(p1.sayHi());
protected 保护类型
class Person {
protected name:string;
protected age:number;
constructor(name:string,age:number){
this.name = name;
this.age = age;
}
sayHi():string{
return `${
this.name}的年龄是${
this.age} --- 当前类(父类)`
}
}
class Son extends Person{
constructor(name:string,age:number){
super(name,age)
}
talk():string{
return `${
this.name}的年龄是${
this.age}---子类`
}
}
var s1 = new Son('张三',99)
console.log(s1.talk())
console.log(s1.sayHi())
var p1 = new Person('康家豪',21);
console.log(p1.name);
class Person {
private name:string;
age:number;
constructor(name:string,age:number){
this.name = name;
this.age = age;
}
sayHi():string{
return `${
this.name}的年龄是${
this.age} --- 当前类(父类)`
}
}
class Son extends Person{
constructor(name:string,age:number){
super(name,age)
}
talk():string{
return `${
this.name}的年龄是${
this.age}---子类`
}
}
var s1 = new Son('张三',99)
console.log(s1.sayHi())
var p1 = new Person('康家豪',21);
console.log(p1.sayHi());
- 类中的静态属性,静态方法,抽象类,继承多态
1.为什么需要静态方法
静态方法通过static关键字来定义,但是静态方法中不能访问类里面的属性
class Person{
public name:string;
static number:number = 99;
constructor(name:string){
this.name = name;
}
run():void{
alert(`${
this.name} 在运动!`)
}
work():void{
alert(`${
this.name} 在工作!`)
}
static print(){
console.log('我是静态类')
}
static sayHi(){
console.log(`${
this.name} hello`);
}
static score(){
console.log(`${
this.number} hello`);
}
}
var p = new Person('张三');
p.run()
p.work()
Person.print();
Person.sayHi();
Person.score();
class Animal{
name:string;
constructor(name:string){
this.name = name;
}
eat():void{
console.log('吃的方法');
}
}
class Dog extends Animal{
constructor(name:string){
super(name);
}
eat():string{
return `${
this.name} 吃肉`
}
}
class Cat extends Animal{
constructor(name:string){
super(name);
}
eat():string{
return `${
this.name} 吃老鼠`
}
}
- TS中的抽象类
//typescript中的抽象类:它是提供其他类继承的基类,不能直接被实例化。
//用abstract关键字定义抽象类和抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类中实现。
//abstract抽象方法只能放在抽象类里面
//抽象类和抽象方法用来定义标准,标准: Animal这个类要求它的子类必须包含eat方法
abstract class Animal{
public name:string;
constructor(name:string){
this.name = name;
}
abstract eat():any;
}
class Dog extends Animal{
constructor(name:string){
super(name);
}
eat(){
console.log(this.name + '吃粮食')
}
}
var d = new Dog('欢欢');
d.eat();