7.封装的属性.js 1.9 KB

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