由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 继续问土问题
相关主题
java Stringhow to print the cllass name of an instance?
求教jbuilder的问题a question on interface
Help: web service questions问HashSet的问题?
how to map one to many relation for a single table in hibe也问个 HashMap问题
问题征解有没有完美的写configuration文件的方案
问个基本问题Re: when do I need to recompile
有熟悉Java Reflection的吗One simple question!
一道java题2 Questions about Constructor
相关话题的讨论汇总
话题: obj话题: new话题: object话题: class2话题: class1
进入Java版参与讨论
1 (共1页)
r*******w
发帖数: 121
1
怎样能够得知一个object的class呢?
就是说我有一个object
我要on the fly new 一个deep copy of it...
so i have to call the related constructor...
but I want the implementation quite generic...
i know i can do
if obj instanceof Class1
then new_obj=new Class1(obj)
else if obj instanceof Class2
then new_obj=new Class2(obj)
etc...
any other more elegant solution?
m******t
发帖数: 2416
2

You can call obj.getClass() to get the Class object for obj.
(quite a tongue twister isn't it)
But you'll have to make sure each of the classes you need to copy
has a default constructor, otherwise you'll still end up checking
classes.

【在 r*******w 的大作中提到】
: 怎样能够得知一个object的class呢?
: 就是说我有一个object
: 我要on the fly new 一个deep copy of it...
: so i have to call the related constructor...
: but I want the implementation quite generic...
: i know i can do
: if obj instanceof Class1
: then new_obj=new Class1(obj)
: else if obj instanceof Class2
: then new_obj=new Class2(obj)

r*******w
发帖数: 121
3
then call newInstance of the Class object?
sounds good ah...
thanks~~~

【在 m******t 的大作中提到】
:
: You can call obj.getClass() to get the Class object for obj.
: (quite a tongue twister isn't it)
: But you'll have to make sure each of the classes you need to copy
: has a default constructor, otherwise you'll still end up checking
: classes.

1 (共1页)
进入Java版参与讨论
相关主题
2 Questions about Constructor问题征解
how to copy an Object?问个基本问题
copy constructor都什么时候be called啊有熟悉Java Reflection的吗
basic java question一道java题
java Stringhow to print the cllass name of an instance?
求教jbuilder的问题a question on interface
Help: web service questions问HashSet的问题?
how to map one to many relation for a single table in hibe也问个 HashMap问题
相关话题的讨论汇总
话题: obj话题: new话题: object话题: class2话题: class1