How to get the parent base class object super.getClass()

后端 未结 5 616
夕颜
夕颜 2020-11-27 07:09

I have a little problem with Java (being a C++ programmer).

I have 2 related classes:

public class Patient() {
...
}

public class PatientPersistent          


        
5条回答
  •  隐瞒了意图╮
    2020-11-27 07:57

    Nice... super.getClass() is actually Object's getClass(), which returns the runtime type of the instance it is invoked on (this in this case). Therefore you receive the same class...

    Instead of asking for the runtime class of this using the super's implementation, You should ask for the super class of the class returned by getClass:

    getClass().getSuperclass()
    

    And by the way, what do you mean by "This will allow me to generalize some methods which I need to implement in each child which is awful."? Are you sure you have no other design choice?

提交回复
热议问题