4_修饰符.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. if (typeof b !== "function" && b !== null)
  10. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  11. extendStatics(d, b);
  12. function __() { this.constructor = d; }
  13. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  14. };
  15. })();
  16. //修饰符:主要是描述类中成员(属性,构造函数,方法)的可访问性
  17. //public 修饰符 类中成员默认的修饰符 代表的公共的任何位置都可以访问类当中的成员
  18. //private 修饰符 类中成员如果使用这个修饰符 外部成员无法访问这个数据 子类也无法访问
  19. //protected 修饰符 类中成员如果使用这个修饰符 外部无法访问数据 子类可以访问数据
  20. (function () {
  21. //定义一个类
  22. var Person = /** @class */ (function () {
  23. // public name: string
  24. // private name: string
  25. // protected name:string
  26. function Person(name) {
  27. this.name = name;
  28. }
  29. Person.prototype.eat = function () {
  30. console.log('我是eat方法', this.name);
  31. };
  32. return Person;
  33. }());
  34. //定义一个子类
  35. var Student = /** @class */ (function (_super) {
  36. __extends(Student, _super);
  37. function Student(name) {
  38. return _super.call(this, name) || this;
  39. }
  40. Student.prototype.play = function () {
  41. console.log('我是play方法', this.name);
  42. };
  43. return Student;
  44. }(Person));
  45. var per = new Person('小红');
  46. console.log(per.name);
  47. })();