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

发布于 2024-09-10 09:04:48 字数 1942 浏览 4 评论 0原文

我对 Java 有一点问题(作为一名 C++ 程序员)。

我有 2 个相关的类:

public class Patient() {
...
}

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

这将输出:

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

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

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

这将使我能够概括一些我需要在每个孩子身上实现的方法,这很糟糕。

谢谢!


更新

我正在使用 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);
}

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

也许我对java太困惑了。

谢谢

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

I have 2 related classes:

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

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

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

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 Patient class. 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.

Maybe I'm just too confused with java.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤单情人 2024-09-17 09:04:49

Object.getClass() 返回对象的运行时类型,因此在调用前添加 super 并没有多大作用。

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

Object.getClass() returns the runtime type of an object, so prepending super to the call doesn't really do much.

You could use getClass().getSuperclass().

千年*琉璃梦 2024-09-17 09:04:49

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

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

指尖凝香 2024-09-17 09:04:49

我发现很多时候,按照您建议的方式用“魔法”解决问题会导致解决方案不太灵活。

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

此外,如果您当前实现的方法实际上在您的父类中,并且该方法引用了在子类中实现的抽象方法,那么设计很有可能会更好——彻底颠覆您的设计。

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

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.

女皇必胜 2024-09-17 09:04:48
getClass().getSuperclass()

但不要使用这个。这无疑是糟糕设计的标志。

getClass().getSuperclass()

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

寄风 2024-09-17 09:04:48

很好... super.getClass() 实际上是 Object 的 getClass(),它返回调用它的实例的运行时类型(在本例中为 this)。因此,您收到相同的类...

而不是使用 super 的实现来请求 this 的运行时类,您应该请求 getClass 返回的类的超类:

getClass().getSuperclass()

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文