zoukankan      html  css  js  c++  java
  • TypeScript

    class Dog {
        // 需要先定义,才能在constructor中this指向
        name: string;
        age: number;
    
        // 构造函数,会在对象创建时调用
        // new Dog() 的时候,就会调用constructor
        constructor(name:string, age:number) {
            /**
             * 在实例方法中,this就表示当前的实例
             * 在构造函数中当前对象就是当前新建的那个对象
             * 可以通过this指向新建的对象中添加属性
             */
    
            this.name = name;
            this.age = age;
        }
        bark(){
            console.log(this.name + " is barking, woofing ")
        }
    }
    const dog = new Dog('Tom', 4);
    console.log(dog);
    const dog2 = new Dog('Max', 2);
    console.log(dog2);
    dog2.bark();
    class Dog {
    // 需要先定义,才能在constructorthis指向
    name: string;
    age: number;

    // 构造函数,会在对象创建时调用
    // new Dog() 的时候,就会调用constructor
    constructor(name:string, age:number) {
    /**
    * 在实例方法中,this就表示当前的实例
    * 在构造函数中当前对象就是当前新建的那个对象
    * 可以通过this指向新建的对象中添加属性
    */

    this.name = name;
    this.age = age;
    }
    bark(){
    console.log(this.name + " is barking, woofing ")
    }
    }
    const dog = new Dog('Tom', 4);
    console.log(dog);
    const dog2 = new Dog('Max', 2);
    console.log(dog2);
    dog2.bark();
  • 相关阅读:
    Vimium -为键盘而生
    Sublime Text 3 修改配色主题【侧边框之...】
    MyBatis-Plus文档地址
    解决:电脑新建文件后需要刷新才显示
    技术书籍博客
    js获取浏览器当前窗口的高度长度
    DataGridView隐藏列用CSS实现
    判断windows操作系统平台
    iis7.5错误 配置错误
    vmware安装mac
  • 原文地址:https://www.cnblogs.com/ningxin/p/15107443.html
Copyright © 2011-2022 走看看