首页 > 技术文章 > es6 super关键字

zhaobao1830 2018-11-12 10:18 原文

rhttp://es6.ruanyifeng.com/#docs/class-extends

super关键字,既可以当作函数使用,也可以当作对象使用。这俩种的使用是不一样的

第一种:函数使用

    代表父类的构造函数。ES6规定,子类的构造函数,必须执行一次super()函数

    注意super虽然代表了父类A的构造函数,但是返回的是子类B的实例,即super内部的this指的是B,因此super()在这里相当于A.prototype.constructor.call(this)

 1 class A {
 2   constructor() {
 3     console.log(new.target.name);
 4   }
 5 }
 6 class B extends A {
 7   constructor() {
 8     super();
 9   }
10 }
11 new A() // A
12 new B() // B

第二种:super作为对象时,在普通方法中,指向父类的原型对象;在静态方法中,指向父类。

 1 class A {
 2   p() {
 3     return 2;
 4   }
 5 }
 6 
 7 class B extends A {
 8   constructor() {
 9     super();
10     console.log(super.p()); // 2
11   }
12 }
13 
14 let b = new B();

ES6 规定,在子类普通方法中通过super调用父类的方法时,方法内部的this指向当前的子类实例。

 1 class A {
 2   constructor() {
 3     this.x = 1;
 4   }
 5   print() {
 6     console.log(this.x);
 7   }
 8 }
 9 
10 class B extends A {
11   constructor() {
12     super();
13     this.x = 2;
14   }
15   m() {
16     super.print();
17   }
18 }
19 
20 let b = new B();
21 b.m() // 2

上面代码中,super.print()虽然调用的是A.prototype.print(),但是A.prototype.print()内部的this指向子类B的实例,导致输出的是2,而不是1。也就是说,实际上执行的是super.print.call(this)

由于this指向子类实例,所以如果通过super对某个属性赋值,这时super就是this,赋值的属性会变成子类实例的属性。

 1 class A {
 2   constructor() {
 3     this.x = 1;
 4   }
 5 }
 6 
 7 class B extends A {
 8   constructor() {
 9     super();
10     this.x = 2;
11     super.x = 3;
12     console.log(super.x); // undefined
13     console.log(this.x); // 3
14   }
15 }
16 
17 let b = new B();

上面代码中,super.x赋值为3,这时等同于对this.x赋值为3。而当读取super.x的时候,读的是A.prototype.x,所以返回undefined

如果super作为对象,用在静态方法之中,这时super将指向父类,而不是父类的原型对象。

 1 class Parent {
 2   static myMethod(msg) {
 3     console.log('static', msg);
 4   }
 5 
 6   myMethod(msg) {
 7     console.log('instance', msg);
 8   }
 9 }
10 
11 class Child extends Parent {
12   static myMethod(msg) {
13     super.myMethod(msg);
14   }
15 
16   myMethod(msg) {
17     super.myMethod(msg);
18   }
19 }
20 
21 Child.myMethod(1); // static 1
22 
23 var child = new Child();
24 child.myMethod(2); // instance 2

上面代码中,super在静态方法之中指向父类,在普通方法之中指向父类的原型对象。

另外,在子类的静态方法中通过super调用父类的方法时,方法内部的this指向当前的子类,而不是子类的实例。

 1 class A {
 2   constructor() {
 3     this.x = 1;
 4   }
 5   static print() {
 6     console.log(this.x);
 7   }
 8 }
 9 
10 class B extends A {
11   constructor() {
12     super();
13     this.x = 2;
14   }
15   static m() {
16     super.print();
17   }
18 }
19 
20 B.x = 3;
21 B.m() // 3

上面代码中,静态方法B.m里面,super.print指向父类的静态方法。这个方法里面的this指向的是B,而不是B的实例。

 

这几天看到类在继承时会用到thissuper,这里就做了一点总结,与各位共同交流,有错误请各位指正~

this

this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针。

this的用法在java中大体可以分为3种:

1.普通的直接引用

这种就不用讲了,this相当于是指向当前对象本身。

2.形参与成员名字重名,用this来区分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Person {
    private int age = 10;
    public Person(){
    System.out.println("初始化年龄:"+age);
}
    public int GetAge(int age){
        this.age = age;
        return this.age;
    }
}
 
public class test1 {
    public static void main(String[] args) {
        Person Harry = new Person();
        System.out.println("Harry's age is "+Harry.GetAge(12));
    }
}       

运行结果:

初始化年龄:10
Harry's age is 12

可以看到,这里age是GetAge成员方法的形参,this.age是Person类的成员变量。

3.引用构造函数

这个和super放在一起讲,见下面。

super

super可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类。

super也有三种用法:

1.普通的直接引用

与this类似,super相当于是指向当前对象的父类,这样就可以用super.xxx来引用父类的成员。

2.子类中的成员变量或方法与父类中的成员变量或方法同名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Country {
    String name;
    void value() {
       name = "China";
    }
}
  
class City extends Country {
    String name;
    void value() {
    name = "Shanghai";
    super.value();      //调用父类的方法
    System.out.println(name);
    System.out.println(super.name);
    }
  
    public static void main(String[] args) {
       City c=new City();
       c.value();
       }
}

运行结果:

Shanghai
China

可以看到,这里既调用了父类的方法,也调用了父类的变量。若不调用父类方法value(),只调用父类变量name的话,则父类name值为默认值null。

3.引用构造函数

super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句)。
this(参数):调用本类中另一种形式的构造函数(应该为构造函数中的第一条语句)。

 

运行结果:

父类·无参数构造方法: A Person.
子类·调用父类”无参数构造方法“: A chinese coder.
父类·含一个参数的构造方法: A person's name is codersai
子类·调用父类”含一个参数的构造方法“: his name is codersai
父类·含一个参数的构造方法: A person's name is codersai
子类·调用父类”含一个参数的构造方法“: his name is codersai
子类:调用子类具有相同形参的构造方法:his age is 18

从本例可以看到,可以用super和this分别调用父类的构造方法和本类中其他形式的构造方法。

例子中Chinese类第三种构造方法调用的是本类中第二种构造方法,而第二种构造方法是调用父类的,因此也要先调用父类的构造方法,再调用本类中第二种,最后是重写第三种构造方法。

 

 1 class Person { 
 2     public static void prt(String s) { 
 3        System.out.println(s); 
 4     } 
 5    
 6     Person() { 
 7        prt("父类·无参数构造方法: "+"A Person."); 
 8     }//构造方法(1) 
 9     
10     Person(String name) { 
11        prt("父类·含一个参数的构造方法: "+"A person's name is " + name); 
12     }//构造方法(2) 
13 } 
14     
15 public class Chinese extends Person { 
16     Chinese() { 
17        super(); // 调用父类构造方法(1) 
18        prt("子类·调用父类”无参数构造方法“: "+"A chinese coder."); 
19     } 
20     
21     Chinese(String name) { 
22        super(name);// 调用父类具有相同形参的构造方法(2) 
23        prt("子类·调用父类”含一个参数的构造方法“: "+"his name is " + name); 
24     } 
25     
26     Chinese(String name, int age) { 
27        this(name);// 调用具有相同形参的构造方法(3) 
28        prt("子类:调用子类具有相同形参的构造方法:his age is " + age); 
29     } 
30     
31     public static void main(String[] args) { 
32        Chinese cn = new Chinese(); 
33        cn = new Chinese("codersai"); 
34        cn = new Chinese("codersai", 18); 
35     } 
36 }

 

super和this的异同:

  • super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句) 
  • this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句)
  • super: 它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时如:super.变量名    super.成员函数据名(实参)
  • this:它代表当前对象名(在程序中易产生二义性之处,应使用this来指明当前对象;如果函数的形参与类中的成员数据同名,这时需用this来指明成员变量名)
  • 调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。
  • super()和this()类似,区别是,super()从子类中调用父类的构造方法,this()在同一类内调用其它方法。
  • super()和this()均需放在构造方法内第一行。
  • 尽管可以用this调用一个构造器,但却不能调用两个。
  • this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。
  • this()和super()都指的是对象,所以,均不可以在static环境中使用。包括:static变量,static方法,static语句块。
  • 从本质上讲,this是一个指向本对象的指针, 然而super是一个Java关键字。

总结:ES6里的super和Java里的区别:(现在只发现一个)
1、es6里的super可以在static里面使用,java里的super不能在static里面使用

推荐阅读