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();
  • 相关阅读:
    luogu P3174 毛毛虫
    P3386二分图最大匹配模版
    P4180 严格次小生成树
    差分约束
    高斯消元
    P1306 斐波那契公约数
    极值
    排序
    P1852 [国家集训队]跳跳棋
    高精度模版
  • 原文地址:https://www.cnblogs.com/ningxin/p/15107443.html
Copyright © 2011-2022 走看看