Inheritance and the “this” keyword

前端 未结 5 1725
有刺的猬
有刺的猬 2020-12-31 09:29

Suppose that we have next situation:

Parent class A:

class A{  
    public A(){}
    public doSomething(){  
        System.out.println(this.getCla         


        
5条回答
  •  星月不相逢
    2020-12-31 10:00

    Despite the fact that you are calling the doSomething() method of A, the this during that call is a B, and so the getClass() method is called on B not on A. Basically the this will always be a B whether you are using a method from superclass A, a method from B, or from A's superclass Object (the parent class of all Java classes).

提交回复
热议问题