zoukankan      html  css  js  c++  java
  • [ts]类

    //
    class Greeter {
      greeting: string;
      constructor(message: string) {
        this.greeting = message;
      }
    
      greet() {
        return `Hello, ${this.greeting}`;
      }
    }
    
    const greeter = new Greeter('world');
    console.log(greeter.greet());
    
    // 继承
    class Animal {
      name: string;
      constructor(theName: string) {
        this.name = theName;
      }
      move(distance: number = 0) {
        console.log(`Animal moved ${distance}m.`);
      }
    }
    class Snake extends Animal {
      constructor(name: string) {
        super(name);
      };
      move(distance: number = 5) {
        console.log('slithering');
        super.move(distance);
      }
    }
    const sam = new Snake('sammy')
    sam.move();
    
    // 公共,私有与受保护的修饰符
    // 默认public
    // private 私有不能在外部访问
    // protected 不能在类外访问,但是可以在子类内访问
    
    class Person {
      protected name: string;
      protected constructor(name: string) { // 构造函数protected,类不能被实例化
        this.name = name;
      }
    }
    class Employee extends Person {
      constructor(name: string) {
        super(name);
      }
      public getName(): string {
        return this.name; // 子类可以访问protected
      }
    }
    
    // readonly
    class Octopus {
      constructor(readonly name: string){}
    }
    const octopus = new Octopus('tom');
    console.log(octopus.name);
    
    // 存取器
    class Emp {
      constructor(private _fullname: string = '') {}
      get fullName(): string {
        return this._fullname;
      }
      set fullName(newName: string) {
        this._fullname = newName + 'aaa';
      }
    }
    const emp = new Emp();
    emp.fullName = 'jerry';
    console.log(emp.fullName);
    
    // 静态属性
    class Grid {
      static origin = { x: 0, y: 0 };
    }
    console.log(Grid.origin);
    
    // 抽象类
    abstract class Animal2 {
      abstract printMethod(): void;
    }
  • 相关阅读:
    23.java方法的深入
    MapReduce:详解Shuffle过程
    Hadoop1.x与Hadoop2的区别
    进程控制的一些api
    程序的静态链接,动态链接和装载
    用户级线程和内核级线程的区别
    从Hadoop框架与MapReduce模式中谈海量数据处理(含淘宝技术架构) (转)
    海量处理面试题
    面试中的二叉树题目
    linux 进程间消息队列通讯
  • 原文地址:https://www.cnblogs.com/zhoulixue/p/15571043.html
Copyright © 2011-2022 走看看