2_继承.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. //A继承了B类,A叫子类,B叫父类
  18. //一旦有的继承的关系 就会出现父子类
  19. (function () {
  20. //定义一个类
  21. var Animal = /** @class */ (function () {
  22. function Animal() {
  23. }
  24. Animal.prototype.run = function (distance) {
  25. console.log("animal run " + distance);
  26. };
  27. return Animal;
  28. }());
  29. //继承
  30. var Dog = /** @class */ (function (_super) {
  31. __extends(Dog, _super);
  32. function Dog() {
  33. return _super !== null && _super.apply(this, arguments) || this;
  34. }
  35. Dog.prototype.cry = function () {
  36. console.log('wang wang!');
  37. };
  38. return Dog;
  39. }(Animal));
  40. var dog = new Dog();
  41. dog.cry();
  42. dog.run('100'); //可以调用从父中继承的方法
  43. //这里 dog是一个派生类 它派生自Animal的基类 通过extends 关键词
  44. })();