super()和this()方法

来源:互联网 发布:javascript的作用 编辑:程序博客网 时间:2024/04/30 16:03

super():表示调用父类的构造方法

this():表示条用本类的其他构造方法

用法:

1,用在类的构造方法中;

2,必须写在构造方法的第一行;(因为类必须构造完成后才能进行其他操作)

3,不可以在一个构造方法中同时使用;(如果同时使用的话,this()会调用父类的构造方法,和super()冲突)


package java.superThis;public class TestSuper {public TestSuper() {System.out.println("super null cons");}public TestSuper(String a) {System.out.println("super name cons");}}
package java.superThis;public class TestThis extends TestSuper{public TestThis() {this("a", "b");System.out.println("this null cons");}public TestThis(String a) {super(a);System.out.println("this name cons");}public TestThis(String a, String b) {super();System.out.println("this name cons");}}
原创粉丝点击