zoukankan      html  css  js  c++  java
  • JS的ES6的class

    1.类的创建:

    • 定义类

    • 类的构造函数

    • 类的静态方法

    • 类的一般属性和方法

     1 //定义类
     2 class Person{
     3 
     4   // 类的静态方法,相当于Person.test = function(){console.log("类的静态方法");}
     5   static test() {
     6     console.log("类的静态方法");
     7     
     8   }
     9 
    10   //constructor构造函数
    11   constructor(name,age){
    12 
    13     console.log("调用构造函数");
    14     this.name = name;
    15     this.age = age;
    16   }
    17 
    18   //类的一般方法,定义在实例对象的原型对象上,相当于Person.prototype.show = function(){console.log("this.name,this.age");}
    19   show(){
    20     console.log(this.name,this.age);
    21     
    22   }
    23 }
    24 
    25 let person1 = new Person("wzh",25);
    26 console.log(person1);
    27 
    28 person1.show();
    29 Person.test();

    2.继承

    • super

    • extends

     1 //定义类
     2 class Person{
     3 
     4   // 类的静态方法,相当于Person.test = function(){console.log("类的静态方法");}
     5   static test() {
     6     console.log("类的静态方法");
     7     
     8   }
     9 
    10   //constructor构造函数
    11   constructor(name,age){
    12 
    13     console.log("调用构造函数");
    14     this.name = name;
    15     this.age = age;
    16   }
    17 
    18   //类的一般方法,定义在实例对象的原型对象上,相当于Person.prototype.show = function(){console.log("this.name,this.age");}
    19   show(){
    20     console.log(this.name,this.age);
    21     
    22   }
    23 }
    24 
    25 let person1 = new Person("wzh",25);
    26 console.log(person1);
    27 
    28 class Child extends Person{
    29 
    30   constructor(name,age,sex){
    31     super(name,age);  //调用父类构造函数构造子类
    32     this.sex = sex;
    33   }
    34 
    35   //重写父类同名函数
    36   show(){
    37     console.log(this.name,this.age,this.sex);
    38     
    39   }
    40 
    41 }
    42 
    43 let child = new Child("wzl",24,"男");
    44 child.show();
  • 相关阅读:
    同一个类生成的对象去重
    关于公众号JavaTokings侵权声明
    消息中间件ActiveMQ使用详解
    重定向和转发的分析与理解
    Oracle SqlPlus导出查询结果
    Sql查询一个列对应多个列
    Jsp标签字典开发_基于Spring+Hibernate
    Oracle数据库导入导出简单备份
    JAVA WEB接口开发简述
    NTKO在线office控件使用实例
  • 原文地址:https://www.cnblogs.com/zhihaospace/p/12070163.html
Copyright © 2011-2022 走看看