Inheritance and the “this” keyword

前端 未结 5 1698
有刺的猬
有刺的猬 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:14

    It doesn't matter what the reference is, it's the instantiated object's class that counts. The object that you're creating is of type B, therefore this.getClass() is always going to return B.

提交回复
热议问题