Java学习——super 与 this 关键字

super 与 this 关键字

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

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

package ti;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Animal a = new Animal();
        a.eat();
        Dog d = new Dog();
        d.eatTest();
    }

}

class Animal {
      void eat() {
        System.out.println("animal : eat");
      }
    }
     
    class Dog extends Animal {
      void eat() {
        System.out.println("dog : eat");
      }
      void eatTest() {
        this.eat();   // this 调用自己的方法
        super.eat();  // super 调用父类方法
      }
    }

 

posted on 2018-10-29 17:02  蔡军帅  阅读(107)  评论(0编辑  收藏  举报