Java——super 与 this 关键字

super 与 this 关键字

super关键字:我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。

this关键字:指向自己的引用。

 1 package ti;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Animal a = new Animal();
 8         a.eat();
 9         Dog d = new Dog();
10         d.eatTest();
11     }
12 
13 }
14 
15 class Animal {
16       void eat() {
17         System.out.println("animal : eat");
18       }
19     }
20      
21     class Dog extends Animal {
22       void eat() {
23         System.out.println("dog : eat");
24       }
25       void eatTest() {
26         this.eat();   // this 调用自己的方法
27         super.eat();  // super 调用父类方法
28       }
29     }
30     
原文地址:https://www.cnblogs.com/wwwwwei/p/9871220.html