6.属性的封装.js 737 B

1234567891011121314151617181920212223242526272829303132
  1. (function () {
  2. class Person {
  3. constructor(name, age, color) {
  4. this.name = name;
  5. this.age = age;
  6. this.color = color;
  7. }
  8. get age1() {
  9. return this.age;
  10. }
  11. set age1(val) {
  12. this.age = val;
  13. }
  14. getValue() {
  15. return this.age + 10;
  16. }
  17. }
  18. class Child extends Person {
  19. look() {
  20. console.log(this.color);
  21. }
  22. }
  23. let p = new Person("孙悟空", 20, '红色');
  24. let c = new Child('图图', 2, '白色');
  25. p.age1 = 30;
  26. console.log(p.age1);
  27. // console.log(p.age);
  28. // console.log()
  29. c.look();
  30. console.log(p.getValue());
  31. // console.log(c.color);
  32. })();