es6 class 클래스 typescript 의 클래스, 문장 3

31372 단어 jsts
클래스 정의
class Person {
  name: string; //         public   ,   public
  constructor(name: string) {
    //     ,        
    this.name = name
  }
  run(): void {
    console.log(this.name + '   ')
  }

  getNane():string {
    return this.name
  }

  setName(name: string):void {
    this.name = name
  }
}

//    
var p = new Person('   ')
p.run() //       
p.getNane() //    
p.setName('   ') // name =    

이어받다
class Person {
  name: string //         public   ,   public
  constructor(name: string) {
    //     ,        
    this.name = name
  }
  run(): void {
    console.log(this.name + '   ')
  }
}

//         extends    ,      super
class Student extends Person {
  constructor(name: string) {
    super(name) //          
  }
  //                 ,             
  work(): void {
  	console.log(this.name + '   ')
  }
  //              ,        ,                  ,     ,       
  run(): void {
    console.log(this.name + '   ')
  }
}

var s = new Student('   ')
s.run() //       
s.work() //       

클래스 안의 장식 부적
//        ,typescript                    
/**
 * public         :    、  、       
 * protected      :    、       。        
 * private        :        。   、      
 *          ,   public
 */

//   public
class Person {
  public name: string
  constructor(name: string) {
    this.name = name
  }
  run(): void {
    console.log(this.name)
  }
}

class Student extends Person {
  constructor(name: string) {
    super(name)
  }
  log(): void {
    console.log(this.name)
  }
}

let p = new Person('   ')
console.log(p.name) //    

let s = new Student('   ')
s.log() //    


//   protected
class Person {
  protected name: string
  constructor(name: string) {
    this.name = name
  }
  run(): void {
    console.log(this.name)
  }
}

class Student extends Person {
  constructor(name: string) {
    super(name)
  }
  log(): void {
    console.log(this.name)
  }
}

let p = new Person('   ')
// protected             ,     js,   ts       
console.log(~~p.name~~ ) //    

let s = new Student('   ')
s.log() //    

//   private
class Person {
  private name: string
  constructor(name: string) {
    this.name = name
  }
  run(): void {
    console.log(this.name)
  }
}

class Student extends Person {
  constructor(name: string) {
    super(name)
  }
  log(): void {
  //           ,            
    console.log(~~this.name~~ )
  }
}

let p = new Person('   ')
p.run() //            name
//           ,            
console.log(~~p.name~~ ) //    

let s = new Student('   ')
s.log() //    

클래스 안의 정적 속성 과 방법
/**
 *         、    
 *           static
 */
class Person {
  name: string
  static code: number = 1
  constructor(name: string) {
    this.name = name
  }
  run(): void {
    console.log(this.name)
  }
  static print(): void {
    //               
    console.log('    ' + ~~this.name~~ ) //       
    //         
    console.log('    ' + Person.code) //     1
    console.log('    ')
  }
}

console.log(Person.code) // 1
Person.print() //     

다 태: 다 태 는 계승 에 속한다
//  :  ,           ,          ,           
class Animal {
  name: string
  constructor(name: string) {
    this.name = name
  }
  //               ,        eat  
  //         ,          eat  
  eat(food: string): void {
    console.log(`${this.name} ${food}`)
  }
}

class Dog extends Animal {
  constructor(name: string) {
    super(name)
  }
}

//   
let dog = new Dog('  ')
dog.eat('  ') //      

class Cat extends Animal {
  constructor(name: string) {
    super(name)
  }
  //           ,         
  eat(food: string): void {
    console.log(`${this.name} ${food},  `)
  }
}

let cat = new Cat('   ')
cat.eat('  ') //       ,  


type: script 의 추상 클래스
/**
 * typescript     ,            ,        
 * abstract             ,                           
 *               
 */
abstract class Animal {
  name: string
  constructor(name: string) {
    this.name = name
  }
  //     
  abstract eat(): void
}

//        ,,     
// let animal = new Animal('   ')      
// abstract    ,                ,        
class Dog extends Animal {
  constructor(name: string) {
    super(name)
  }
  //       ,            
  eat(): void {
    console.log(this.name + '   ')
  }
}

let dog = new Dog('  ')
dog.eat() //      

좋은 웹페이지 즐겨찾기