简体   繁体   English

如何获取父基类对象super.getClass()

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

I have a little problem with Java (being a C++ programmer). 我有一点Java问题(作为一名C ++程序员)。

I have 2 related classes: 我有2个相关的课程:

public class Patient() {
...
}

public class PatientPersistent extends Patient {
...
    public void foo() {
    System.out.println(super.getClass().toString());
    }
}

This will output: 这将输出:

class org.example.smartgwt.server.model.PatientPersistent class org.example.smartgwt.server.model.PatientPersistent

Is there a way to get the parent class type? 有没有办法获得父类类型? ie

class org.example.smartgwt.server.model.Patient. class org.example.smartgwt.server.model.Patient。

This will allow me to generalize some methods which I need to implement in each child which is awful. 这将允许我概括一些我需要在每个孩子中实施的方法,这些方法很糟糕。

Thanks! 谢谢!


UPDATE UPDATE

I'm using Dozer to convert my domain Hibernate object to a Serializable version. 我正在使用Dozer将我的域Hibernate对象转换为Serializable版本。 I don't want the client to know of this, so the client only sees the Patient class. 我不希望客户知道这一点,因此客户只能看到Patient类。 On the server side I perform conversions. 在服务器端,我执行转换。

public class DataObject<Type> {

    private static final Class<Object> DstType = Type;

    public Object convert(Object srcData, final BeanFactory factory) {
        Mapper mapper = (Mapper)factory.getBean("dozerMapper");

        return (Object)mapper.map(srcData, DstType);
    }
}

public class Patient() implements Serializable {
    public Set foo;
}    

public class PatientPersistent extends Patient {

    public org.hibernate.collection.PersistentSet foo;
    DataObject<Patient> converter = new DataObject<Patient>;

    public Patient convertToSerializable(final BeanFactory factory) {
        return (Patient)converter.convert(this, factory);
    }
}

public class main() {
    // This object is not serializable so I cannot send it to the client
    PatientPersistent serializableBar = new PatientPersistent();

    // Using Dozer to copy the data PatientPersistent -> Patient
    // This will load the Dozer spring bean and copy as mapped
    Patient copiedSerializableData = serializableBar.convertToPersistent(bar, factory);
}

I know this code does not work, but it's just to make my point. 我知道这段代码不起作用,但这只是为了说明我的观点。 I would like to be able to convert the object to it's serializable form so that I can send it back to the client. 我希望能够对象转换为可序列化的形式,以便我可以将其发送回客户端。 That's why I would like to give the parent's type. 这就是为什么我想给父母的类型。 Calling the mapper will always be the same thing, a source object and a Dest.class. 调用映射器将始终是相同的东西,源对象和Dest.class。

Maybe I'm just too confused with java. 也许我对java太困惑了。

Thanks 谢谢

getClass().getSuperclass()

But don't use this. 但是不要使用它。 It is certainly a sign of bad design. 这肯定是糟糕设计的标志。

Nice... super.getClass() is actually Object's getClass(), which returns the runtime type of the instance it is invoked on ( this in this case). 尼斯... super.getClass()实际上是对象的的getClass(),它返回它是在调用的实例的运行时类型( 在这种情况下)。 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返回的类:

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? 您确定没有其他设计选择吗?

Object.getClass() returns the runtime type of an object, so prepending super to the call doesn't really do much. Object.getClass()返回一个对象的运行时类型,因此在调用前加上super对实际上没有多大帮助。

You could use getClass().getSuperclass() . 你可以使用getClass().getSuperclass()

尝试: getClass().getSuperclass().toString()

I find a lot of time that solving a problem with "magic" in the way you are suggesting leads to a solution that is not very flexible. 我发现很多时候以你建议的方式用“魔法”解决问题会导致解决方案不灵活。

If you are trying to get your super's class, what you probably want is to be calling a method in your parent's class that explicitly implements whatever action you were going to take after you looked up the parent's type. 如果你想要获得你的超级课程,你可能想要的是在你的父类中调用一个方法,该方法明确地实现你在查找父类型后要采取的任何动作。

Furthermore, there is a good chance that the design would be better yet if the method you were currently implementing were actually IN your parent class and that method was referring to an abstract method implemented in the child class--flipping your design on its head. 此外,如果您当前实现的方法实际上是在您的父类中,并且该方法指的是在子类中实现的抽象方法 - 在您的头上翻转您的设计,那么设计很可能会更好。

Of course I'm inferring this all from your desire to get the parent type and may be totally wrong, but I do think that if knowing your parent's class is the answer you're looking for then you are asking the wrong question--back up a few feet and look again. 当然我从你想要获得父母类型的过程中推断这一切并且可能完全错误,但我认为如果你知道你父母的课程是你正在寻找的答案那么你就会问错误的问题 - 返回向上几英尺再看一遍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM