TypeScript 之構造器 constructor 方法 methods


class Movie {
  name: string;
  play_count: number;
  create_at: string;
  constructor(name: string, play_count: number = 12, create_at: string) {
    // this 指向生成點 Object 本身
    this.name = name;
    this.play_count = play_count;
    this.create_at = create_at;
  }
 
  // methods 可以對 data 進行操作
  display_play_count(padding: string = '***') {
    return this.play_count + '次' + padding    
  }
  increase_play_count() {
    this.play_count += 1;
  }
}

let a = new Movie('阿麗塔:戰斗天使', undefined, '17點28分');

a.increase_play_count();  // 13***  雖然第二個參數並沒有傳遞 可以使用 undefined 來占位 會使用默認值 12 再 += 1

console.log(a, a.display_play_count());  // Movie { name: '阿麗塔:戰斗天使', play_count: 13, create_at: '17點28分' } '13次***'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM