如何获取父基类对象super.getClass() [英] How to get the parent base class object super.getClass()
问题描述
我对Java有一个问题(是一个C ++程序员)。
我有两个相关的类:
public class Patient(){
...
}
public class PatientPersistent extends Patient {
.. 。
public void foo(){
System.out.println(super.getClass()。toString());
}
}
这将输出:
class org.example.smartgwt.server.model.PatientPersistent
有没有办法获得父类类型?即
class org.example.smartgwt.server.model.Patient。
这将允许我推广一些方法,我需要在每个孩子是可怕的实现。
谢谢!
UPDATE
将我的域Hibernate对象转换为可序列化版本。我不希望客户知道这一点,因此客户端只看到病人类。在服务器端我执行转换。
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(){
//此对象不可序列化,因此无法将其发送到客户端
PatientPersistent serializableBar = new PatientPersistent();
//使用Dozer来复制数据PatientPersistent - > Patient
//这将加载Dozer弹簧bean并复制为映射
Patient copiedSerializableData = serializableBar.convertToPersistent(bar,factory);
}
我知道这个代码不工作,但它只是为了说明我的观点。我想能够将转换为对象的可序列化形式,以便我可以将其发送回客户端。这就是为什么我想给父母的类型。调用 mapper 将始终是相同的事情,一个源对象和一个Dest.class。
也许我只是对java混淆。
感谢
getClass()。getSuperclass()
但不要使用。这当然是坏设计的标志。
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
getClass().getSuperclass()
But don't use this. It is certainly a sign of bad design.
这篇关于如何获取父基类对象super.getClass()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!