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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3294656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:49:43  来源:igfitidea点击:

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

javaclassdozer

提问by code-gijoe

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

类 org.example.smartgwt.server.model.PatientPersistent

Is there a way to get the parent class type? i.e.

有没有办法获取父类类型?IE

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

类 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

更新

I'm using Dozer to convert my domain Hibernate object to a Serializable version. I don't want the client to know of this, so the client only sees the Patientclass. On the server side I perform conversions.

我正在使用 Dozer 将我的域 Hibernate 对象转换为可序列化版本。我不想让客户知道这一点,所以客户只能看到Patient类。在服务器端,我执行转换。

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 convertthe 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 mapperwill always be the same thing, a source object and a Dest.class.

我知道这段代码不起作用,但这只是为了表明我的观点。我希望能够对象转换为可序列化的形式,以便我可以将其发送回客户端。这就是为什么我想给父母的类型。调用映射器总是一样的,一个源对象和一个 Dest.class。

Maybe I'm just too confused with java.

也许我对java太困惑了。

Thanks

谢谢

采纳答案by Bozho

getClass().getSuperclass()

But don't use this. It is certainly a sign of bad design.

但是不要用这个。这当然是糟糕设计的标志。

回答by Mike Tunnicliffe

Try: getClass().getSuperclass().toString()

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

回答by Mike Daniels

Object.getClass()returns the runtimetype of an object, so prepending superto the call doesn't really do much.

Object.getClass()返回一个对象的运行时类型,所以super在调用之前并没有做太多事情。

You could use getClass().getSuperclass().

你可以使用getClass().getSuperclass().

回答by Eyal Schneider

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

尼斯... super.getClass()实际上是对象的的getClass(),它返回它被调用的实例的运行时类型(在这种情况下)。因此,您收到相同的课程...

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

而不是使用 super 的实现来请求this的运行时类,您应该请求 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?

顺便说一句,“这将允许我概括一些我需要在每个孩子身上实施的方法,这很糟糕。”是什么意思?您确定没有其他设计选择吗?

回答by Bill K

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.

如果您试图获取 super 的类,您可能想要的是调用父类中的一个方法,该方法显式实现您在查找父类的类型后将要采取的任何操作。

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.

当然,我是从你想要获得父类型的愿望中推断出这一切的,这可能是完全错误的,但我确实认为,如果知道你父母的班级是你正在寻找的答案,那么你就问错了问题——返回向上几英尺再看。