1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- var __extends = (this && this.__extends) || (function () {
- var extendStatics = function (d, b) {
- extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
- return extendStatics(d, b);
- };
- return function (d, b) {
- if (typeof b !== "function" && b !== null)
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- // 多态: 父类型的引用指向了子类型的对象 ,不同类型的对象针对相同的方法,产生了不同的行为
- (function () {
- //定义一个父类
- var Animal = /** @class */ (function () {
- //定义一个构造函数
- function Animal(name) {
- this.name = name;
- }
- //方法
- Animal.prototype.run = function (distance) {
- if (distance === void 0) { distance = 0; }
- console.log("run " + distance + " far away", this.name);
- };
- return Animal;
- }());
- //定义一个子类
- var Dog = /** @class */ (function (_super) {
- __extends(Dog, _super);
- //构造函数
- function Dog(name) {
- //调用父类的构造函数 实现子类中属性的初始化操作
- return _super.call(this, name) || this;
- }
- //实例的方法
- Dog.prototype.run = function (distance) {
- if (distance === void 0) { distance = 10; }
- console.log("run " + distance + " far away", this.name);
- };
- return Dog;
- }(Animal));
- var Pig = /** @class */ (function (_super) {
- __extends(Pig, _super);
- //构造函数
- function Pig(name) {
- return _super.call(this, name) || this;
- }
- Pig.prototype.run = function (distance) {
- if (distance === void 0) { distance = 20; }
- console.log("run " + distance + " far away", this.name);
- };
- return Pig;
- }(Animal));
- //实例化父类的对象
- var ani = new Animal('动物');
- ani.run();
- //实例化子类对象
- var dog = new Dog('大黄');
- dog.run();
- var pig = new Pig('佩奇');
- pig.run();
- /* 父类和子类的关系 可以通过父类的类型 创建子类的类型 */
- var dog1 = new Dog('小黄');
- var pig1 = new Pig('乔治');
- dog1.run();
- pig1.run();
- /* 函数 */
- function showRun(ani) {
- ani.run();
- }
- showRun(dog1);
- showRun(pig1);
- })();
|