赶时间的看总结就好了。
异同
super
super();
调用父类构造方法;super.date;
调用父类的属性;super.func();
调用父类的方法。
this
this();
调用构造方法this.date;
调用本类属性;this.func();
调用本类方法。
代码示例
super
public class TestSuperAndThis extends Person{
public int length;
public String name;
public TestSuperAndThis(int length, String name) {
super(name);
this.length = length;
this.name = name;
}
public void function() {
super.method();
System.out.println(super.name);
}
public static void main(String[] args) {
TestSuperAndThis test = new TestSuperAndThis(18,"逍遥");
test.function();
}
}
class Person {
/**
* 3. 调用父类的属性
*/
public String name;
/**
* 1. 调用父类构造方法
* @param name
*/
public Person(String name) {
System.out.println(name + "调用了父类的构造方法");
}
/**
* 2. 调用父类的普通方法
*/
public void method() {
System.out.println("调用父类的普通方法");
}
}
逍遥调用了父类的构造方法
调用父类的普通方法
null
this
public class TestSuperAndThis {
public int length;
public String name;
/**
* 1. 调用本类的构造方法
* @param name
*/
public TestSuperAndThis(String name) {
this();
}
public TestSuperAndThis() {
System.out.println("调用本类的其他构造方法");
}
/**
* 2. 调用本类的普通方法
*/
public void function() {
this.method();
}
/**
* 3. 调用本类的属性
*/
public void method() {
System.out.println("调用本类的普通方法");
String newName = this.name;
System.out.println(newName);
}
public static void main(String[] args) {
TestSuperAndThis test = new TestSuperAndThis("逍遥");
test.function();
}
}
调用本类的其他构造方法
调用本类的普通方法
null