1.类.ts 565 B

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * 用class去声明类
  3. * 属性 方法
  4. * 使用:实例化对象
  5. * static 静态属性 静态方法
  6. * 修改只能使用类去修改
  7. * 规避name字段
  8. * readonly 只读属性
  9. */
  10. class Person {
  11. // 属性
  12. readonly name:string = '张三';
  13. static age:number = 18;
  14. // 方法 ;类方法
  15. static say() {
  16. console.log('大家好');
  17. }
  18. }
  19. // 实例化对象
  20. let p1 = new Person();
  21. console.log(p1,'1');
  22. // p1.name = '李四';
  23. // p1.age = 20;
  24. Person.age = 20;
  25. console.log(p1,'2')
  26. // p1.say();
  27. console.log(Person.age);
  28. Person.say();