通过看代码注释就可以很简单的了解super和this在java中的作用了。
super:实现对父类成员的访问,用于引用当前对象的父类。
This:指向自己的引用
你有一辆别克,然后谈了一个女朋友,女朋友只看到你的this.别克,然后不愿意和你在一起,然后你告诉女朋友你可以extends你父亲,,就可以super.奥迪A8
当父类的方法与子类同名时:在实例化子类后调用该方法时会使用子类写的方法内容,要用父类的同名方法需要super.方法名
当父类的方法与子类不同名时:实例化父类后只能调用父类的方法,实例化子类后写的那个方法名就调用哪个方法(无论是父类还是子类的方法都能调用),,
package niaocai;
/*
* @父类Animal
*/
class Animal{
void eat() {
System.out.println("i'm father eat");
}
void eatt() {
System.out.println("i'm father eatt");
}
}
/*
* super继承的是父类的方法;当子类对父类的方法进行重写的时候
* (1)想要使用父类的方法应当使用super.方法名
* (2)使用子类的方法时可以使用this.方法名,也可以直接写方法名,
* 何时使用????当父类和子类的方法名重名时
* 当直接写方法名时会直接调用该类当中的方法,要想使用父类中的某方法应使用super.方法名
*/
/*
* @子类Dog继承Animal
*/
class Dog extends Animal{
void eat() {
System.out.println("i'm son eat");
}
void eatTest() {
this.eat();//调用自己的方法 this
super.eat();//调用父类的方法 super
System.out.println();
eat();
eatt();
}
}
public class SuperThis {
public static void main(String[] args) {
Animal a=new Animal();
a.eat();
Dog D=new Dog();
D.eat();
D.eatTest();
}
}