7.封装的属性.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // 属性 封装:令属性更安全
  2. (function(){
  3. /**
  4. * readonly 只读
  5. * public 公共的
  6. * private 私有的 只能在当前类中进行使用(访问及修改)
  7. * protected 受保护的 当前字段在当前类及当前子类中只能访问
  8. */
  9. // class Person {
  10. // public name:string;
  11. // private age:number;
  12. // constructor(name:string,age:number) {
  13. // this.name = name;
  14. // this.age = age;
  15. // }
  16. // /***
  17. // * 属性封装
  18. // * getter 获取属性值 get
  19. // * setter 设置属性值 set
  20. // */
  21. // get name1() {
  22. // return this.age+'1';
  23. // }
  24. // set name1(value:string) {
  25. // this.name = value
  26. // }
  27. // // getName() {
  28. // // return this.name;
  29. // // }
  30. // // setName(value:string) {
  31. // // return this.name = value;
  32. // // }
  33. // }
  34. // let p = new Person("孙悟空",10)
  35. // // p.setName("猪八戒")
  36. // // console.log(p.getName(),'实例化对象')
  37. // console.log(Person,'类')
  38. // // p.age = 12;
  39. // p.name1 = '唐僧'
  40. // console.log(p.name1)
  41. // class A {
  42. // public name:string;
  43. // public age:number;
  44. // constructor(name:string,age:number) {
  45. // this.name = name;
  46. // this.age = age;
  47. // }
  48. // }
  49. // class B {
  50. // constructor( public name:string,public age:number) {
  51. // this.name = name;
  52. // this.age = age;
  53. // }
  54. // }
  55. class C {
  56. protected num:number;
  57. constructor(num:number) {
  58. this.num = num;
  59. }
  60. }
  61. class D extends C {
  62. sayHello() {
  63. console.log("你好")
  64. }
  65. }
  66. let c = new C(12);
  67. console.log(c,'c')
  68. // c.num = 99;
  69. let d = new D(23);
  70. console.log(d)
  71. // d.num = 88;
  72. })()