2.构造函数和this.ts 308 B

1234567891011121314151617181920
  1. // 构造函数
  2. // function fn1() {
  3. // };
  4. // fn1();
  5. // new fn1();
  6. class Person1 {
  7. name:string;
  8. age:number;
  9. constructor(name:string,age:number) {
  10. this.name = name;
  11. this.age = age;
  12. }
  13. hello() {
  14. console.log("hi")
  15. }
  16. }
  17. let p2 = new Person1('八戒',10);
  18. p2.hello()