zoukankan      html  css  js  c++  java
  • es6 语法 (类与对象)

    {
      // 基本定义和生成实例
      class Parent{
        constructor(name='mukewang'){
          this.name=name;
        }
      }
      let v_parent1=new Parent();
      let v_parent2=new Parent('v');
      console.log('构造函数和实例',v_parent1,v_parent2); // Parent {name: "mukewang"};Parent {name: "v"}
    }
    
    {
      // 继承
      class Parent{
        constructor(name='mukewang'){
          this.name=name;
        }
      }
    
      class Child extends Parent{
    
      }
    
      console.log('继承',new Child());//Child {name: "mukewang"}
    }
    
    {
      // 继承传递参数
      class Parent{
        constructor(name='mukewang'){
          this.name=name;
        }
      }
    
      class Child extends Parent{
        constructor(name='child'){
          super(name);
          this.type='child';
        }
      }
    
      console.log('继承传递参数',new Child('hello')); //_Child {name: "hello", type: "child"}
    }
    
    {
      // getter,setter
      class Parent{
        constructor(name='mukewang'){
          this.name=name;
        }
    
        get longName(){
          return 'mk'+this.name
        }
    
        set longName(value){
          this.name=value;
        }
      }
    
      let v=new Parent();
      console.log('getter',v.longName);//mkmukewang
      v.longName='hello';
      console.log('setter',v.longName);//mkhello
    }
    
    {
      // 静态方法
      class Parent{
        constructor(name='mukewang'){
          this.name=name;
        }
    
        static tell(){
          console.log('tell'); 
        }
      }
    
      Parent.tell(); //tell
    
    }
    
    {
      // 静态属性
      class Parent{
        constructor(name='mukewang'){
          this.name=name;
        }
    
        static tell(){
          console.log('tell');
        }
      }
    
      Parent.type='test';
    
      console.log('静态属性',Parent.type); //test
    
    
    }
  • 相关阅读:
    [POJ 1050]To the Max
    P1678 烦恼的高考志愿
    P1873 砍树
    P1102 A-B 数对
    P6771 [USACO05MAR]Space Elevator 太空电梯
    P2347 砝码称重
    P1832 A+B Problem(再升级)
    P1679 神奇的四次方数
    P1877 [HAOI2012]音量调节
    P1049 装箱问题
  • 原文地址:https://www.cnblogs.com/Byme/p/7718627.html
Copyright © 2011-2022 走看看