7.属性的封装.ts 897 B

1234567891011121314151617181920212223242526272829303132333435
  1. (function() {
  2. /**
  3. * readonly
  4. * static
  5. * private 私有的 只能使用类去进行获取修改
  6. * public 共有的
  7. * protected 受保护的 只能在当前类或者当前类的子类中访问
  8. */
  9. class Person {
  10. private name:string;
  11. protected age:number;
  12. constructor(name:string,age:number) {
  13. this.name = name;
  14. this.age = age;
  15. }
  16. getName() {
  17. return console.log(this.name)
  18. }
  19. get name1() {
  20. return this.name;
  21. }
  22. set name1(val) {
  23. this.name = val;
  24. }
  25. }
  26. let p = new Person("蜡笔小新",3);
  27. console.log(p,'修改前')
  28. // p.name = '图图';
  29. // console.log(p.name,'修改后')
  30. console.log(Person.name,'哈哈')
  31. p.getName()
  32. p.name1 = '图图';
  33. // p.age = 12;
  34. // console.log(p.name1,p.age)
  35. })()