typescript的类

116 阅读3分钟
  • es5里面的类
  • ts里面的类

es5里面的类

es5创建对象继承 1.最简单的类

function Person(){
  this.name = '张三'
  this.age = 20
  this.address={

  }
}
var p1 = new Person()

2.构造函数和原型链里面增加方法

function Person(){
  this.name = '张三'/*属性*/
  this.age = 20
  this.address={

  }
  this.run = function(){
    alert(this.name+'在运动')
  }
}
// 原型链上的属性会被多个构造函数共享 构造函数不会
Person.prototype.sex = '男'
Person.prototype.work = function(){
  alert(this.name + '在工作')
}
var p1 = new Person()
p1.work()

3.类里面的静态方法

function Person(){
  this.name = '张三'/*属性*/
  this.age = 20
  this.address={

  }
  this.run = function(){ /*实例方法*/
    alert(this.name+'在运动')
  }
}
Person.getInfo = function(){
  alert('我是静态方法')
}
// 原型链上的属性会被多个构造函数共享 构造函数不会
Person.prototype.sex = '男'
Person.prototype.work = function(){
  alert(this.name + '在工作')
}
var p1 = new Person()
p1.work()
Person.getInfo()

4.es5里面的继承

对象冒充

function Person(){
  this.name = '张三'/*属性*/
  this.age = 20
  this.run = function(){ /*实例方法*/
    alert(this.name+'在运动')
  }
}

Person.prototype.sex = '男'
Person.prototype.work = function(){
  alert(this.name + '在工作')
}

// web类继承Person类 原型链+对冒充的组合继承模式
function Web(){
  Person.call(this) //对象冒充实现继承构造函数里面的属性和方法
}
var w = new Web()
w.work() // 没法继承原型链上的属性和方法 

原型链继承

function Person(name,age){
  this.name = name/*属性*/
  this.age = age
  this.run = function(){ /*实例方法*/
    alert(this.name+'在运动')
  }
}
Person.prototype.sex = '男'
Person.prototype.work = function(){
  alert(this.name + '在工作')
}
function Web(name,age){}
Web.prototype = new Person()

var w = new Web('赵四',20);
var w = new Web('王五',20);
w.run()
//原型链继承
// 优点 可以继承构造函数里面的属性和方法又能继承原型上的属性和方法 
// 问题  实例化子类的时候没法给父类传参

组合继承 原型链+构造函数的组合继承模式

function Person(name,age){
  this.name = name/*属性*/
  this.age = age
  this.run = function(){ /*实例方法*/
    alert(this.name+'在运动')
  }
}
Person.prototype.sex = '男'
Person.prototype.work = function(){
  alert(this.name + '在工作')
}
function Web(name,age){
  Person.call(this,name,age) // 对象冒充继承 实例化子类 可以给父类传参
}
Web.prototype = new Person()
var w = new Web('赵四',20)
w.run()
w.work()
function Person(name,age){
  this.name = name/*属性*/
  this.age = age
  this.run = function(){ /*实例方法*/
    alert(this.name+'在运动')
  }
}
Person.prototype.sex = '男'
Person.prototype.work = function(){
  alert(this.name + '在工作')
}
function Web(name,age){
  Person.call(this,name,age) // 对象冒充继承 实例化子类 可以给父类传参构造函数 

}
Web.prototype = Person.prototype
var w = new Web('赵四',20)
w.run()
w.work()

ts中定义类

class Person{
  name:string; //属性前面省略了punblic关键词
  constructor(n:string){
    //构造函数 实例化类的时候触发的方法
    this.name = n;
  }
  run():void{
    alert(this.name)
  }
}
var p = new Person('张三')
p.run()

class Person{
  name:string=''; //属性前面省略了punblic关键词
  constructor(name?:string){
    //构造函数 实例化类的时候触发的方法
    if(name){
      this.name = name;
    }
    
  }
  getName():string{
   return this.name
  }
  setName(name:string):void{
    this.name = name
  }
}
var p = new Person('张三')
alert(p.getName())
p.setName('李四')
var p3 = new Person()
alert(p3.getName())

ts中实现继承 extends super

class Person{
  name:string;
  constructor(name:string){
    this.name = name
  }
  run():string{
    return `${this.name}在运动`
  }
}
var p =new Person('王五')
alert(p.run())

class Web extends Person{
  constructor(name:string){
    super(name)
  }
}
var w = new Web('李四')
alert(w.run())

class Person{
  name:string;
  constructor(name:string){
    this.name = name
  }
  run():void{
    alert(`${this.name}在运动`)
  }
}
var p =new Person('王五')

class Web extends Person{
  constructor(name:string){
    super(name)
  }
  run():void{
    alert(`${this.name}在运动子类`)
  }
  work(){
    alert(`${this.name}在工作`)
  }
}
var w = new Web('李四')
w.work()

类里面的修饰符 typescript里面第一属性的时候给我们提供了三种修饰符

public 共有 在类里面 子类 类外面都可以访问

protected 保护类型 在类里面子类里面可以访问 在类外部不能访问

private 只能在类里面访问,子类和类外部都不能访问

public代码

class Person{
  public name:string;
  constructor(name:string){
    this.name = name
  }
  run():void{
    alert(`${this.name}在运动`)
  }
}

class Web extends Person{
  constructor(name:string){
    super(name)
  }
  run():void{
    alert(`${this.name}在运动子类`)
  }
  work(){
    alert(`${this.name}在工作`)
  }
}


var p = new Person('李四')
alert(p.name)

var w = new Web('王五')
w.work()