zoukankan      html  css  js  c++  java
  • Typescript的面向对象

    封装:

    var Greeter = (function () {
        function Greeter(message) {
            this.greeting = message;
        }
        Greeter.prototype.greet = function () {
            return "Hello, " + this.greeting;
        };
        return Greeter;
    })();
    var greeter = new Greeter("world");
    var button = document.createElement('button');
    button.textContent = "Say Hello";
    button.onclick = function () {
        alert(greeter.greet());
    };
    document.body.appendChild(button);

    继承:

    var __extends = this.__extends || function (d, b) {
        for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
        function __() { this.constructor = d; }
        __.prototype = b.prototype;
        d.prototype = new __();
    };
    var Animal = (function () {
        function Animal(name) {
            this.name = name;
        }
        Animal.prototype.move = function (meters) {
            alert(this.name + " moved " + meters + "m.");
        };
        return Animal;
    })();
    var Snake = (function (_super) {
        __extends(Snake, _super);
        function Snake(name) {
            _super.call(this, name);
        }
        Snake.prototype.move = function () {
            alert("Slithering...");
            _super.prototype.move.call(this, 5);
        };
        return Snake;
    })(Animal);
    var Horse = (function (_super) {
        __extends(Horse, _super);
        function Horse(name) {
            _super.call(this, name);
        }
        Horse.prototype.move = function () {
            alert("Galloping...");
            _super.prototype.move.call(this, 45);
        };
        return Horse;
    })(Animal);
    var sam = new Snake("Sammy the Python");
    var tom = new Horse("Tommy the Palomino");
    sam.move();
    tom.move(34);

    多态:

    控制反转,父类的实例,子类的功能。 

    闭包,就是面向对象里面的类。

  • 相关阅读:
    Java Image Processing
    贝塞尔曲线开发的艺术
    Ubuntu中Hadoop环境搭建
    FIRST集合、FOLLOW集合、SELECT集合以及预测分析表地构造
    Linux环境下使用VSCode编译makefile文件的注意事项
    神经记忆模型
    深度学习推荐阅读的论文
    博客园无法发布文章解决办法
    计算机各个方向名校公开课
    软件过程基础
  • 原文地址:https://www.cnblogs.com/samwu/p/4354370.html
Copyright © 2011-2022 走看看