Thanks a lot, also to Andrew T. :P
> > If I have a String "Hello" and I know it is a name of a class, how to
> > use the string to create a object?
[quoted text clipped - 6 lines]
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#forName(java.lang.String)>
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#newInstance()>
A following question,
It can run, but IF the class "Hello" have a method get();
using o.get() is wrong. Because it is a Object, not a Class. How to
solve then?
Thanks a lot
Fans.
Thomas Schodt 写道:
> > If I have a String "Hello" and I know it is a name of a class, how to
> > use the string to create a object?
[quoted text clipped - 6 lines]
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#forName(java.lang.String)>
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#newInstance()>
Dijon Yu - 26 Sep 2006 06:59 GMT
> A following question,
> It can run, but IF the class "Hello" have a method get();
>
> using o.get() is wrong. Because it is a Object, not a Class. How to
> solve then?
If method get() is not a static method, you must create a instance of
Hello class, you use Object o =
Class.forName(classname).newInstance();
like Thomas Schodt write
Dijon Yu
> Thomas Schodt 写道:
>
[quoted text clipped - 8 lines]
> > <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#forName(java.lang.String)>
> > <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#newInstance()>
Thomas Schodt - 26 Sep 2006 07:32 GMT
> A following question,
> It can run, but IF the class "Hello" have a method get();
>
> using o.get() is wrong. Because it is a Object, not a Class. How to
> solve then?
You either need to know about the class (which defeats the purpose)
or it needs to implement a known interface
interface KnownInterface {
Object get();
}
class UnknownClass implements KnownInterface {
Object get() {
// ...
}
}
String classname = "UnknownHello";
Object o = Class.forName(classname).newInstance();
KnownInterface ki = (KnownInterface)o;
Object ret = ki.get();
or you need to get serious with reflection - read
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/package-summary.html>
fAnSKyer - 27 Sep 2006 22:47 GMT
Thanks a lot.
What if KnownInterface is also a String and I need use this String to
do the following steps?
Still need help.
Thanks, fAns.
Thomas Schodt 写道:
> > A following question,
> > It can run, but IF the class "Hello" have a method get();
[quoted text clipped - 22 lines]
> or you need to get serious with reflection - read
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/package-summary.html>
Thomas Schodt - 28 Sep 2006 10:06 GMT
> What if KnownInterface is also a String
Then it is not a known interface. o_O
> Still need help.
you need to get serious with reflection - read
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/reflect/package-summary.html>
There seems to be a number of threads around on this.
Read them.
su_dang@hotmail.com - 28 Sep 2006 16:22 GMT
> A following question,
> It can run, but IF the class "Hello" have a method get();
[quoted text clipped - 17 lines]
> > <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#forName(java.lang.String)>
> > <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#newInstance()>
Using reflection, you can obtain an object of the class Method which
can be used to invoke the method you want.
fAnSKyer - 02 Oct 2006 04:51 GMT
thanks and those who helped me :P
> > A following question,
> > It can run, but IF the class "Hello" have a method get();
[quoted text clipped - 20 lines]
> Using reflection, you can obtain an object of the class Method which
> can be used to invoke the method you want.