Neighborhoods Class.forName () usage

Category: Java SE
 
d527929639
2010-02-11 08:06:50

Sponsored Links
The following are some forName () using code:

public void print () {

try {
Object _obj = Class.forName (" ; application.test.javaclass.PrintJavaClassHello "). newInstance ();
System.out.println (_obj.getClass (). getName ());

/ / must also be explicitly casts, can not meet the requirements of the workflow ((PrintJavaClassHello) _obj). printHello ();

;} catch (ClassNotFoundException e1) {}
catch ; (InstantiationException e2) {}
catch (IllegalAccessException e3) {}
}
problem: As I annotated in the code, and if I do not PrintJavaClassHello mandatory conversion, but also the method call PrintJavaClassHello printHello () do? If this does not, then what kind of solutions can achieve this it?


Sponsored Links

xinhengli
2010-02-11 08:15:10
If you can not use dynamically loaded cast type this method. Because we do not know in the end of this class is Class in the end what class. Also said that if you use this code:
String className = "MyClass";
Clsss iClass = Class.forName (className);
Object iObj = iClass . newInstance ();
generated iObj, you really can not use classname to dynamically convert.
If you want to use this generated object should be how to do it?
I assume you MyClass defined as follows.
public class MyClass
{
public MyClass () {
}
public void sayHello (String who)
{
System.out.println ("hello world" + who);
}
}
If you want to use dynamic sayHello, you can use the following code
import java.lang.reflect. *;
class TestClass
{
public static void main (String [] args)
{
/ / This is the sayHello parameters of type String
Class [] parameterTypes = {String.class};
/ / ready to put the string you pass into
Object [] parameter = {"you"};
; / / The following is a dynamically loaded class name and function name you want to call
String className = "MyClass";
String methodName = "sayHello";
try {
Class iClass = Class.forName (className);
; / / Generate a Method object (sayHello)
Method method = iClass. getMethod (methodName, parameterTypes);
/ / Class . forname just dynamically loaded class, we also need to generate object, so
; / / need to call its constructor with the following method call
Constructor con = iClass.getConstructor (new Class [] {});
Object myclass = con . newInstance (new Object [] {});
/ / object to generate it, and now it is called
method.invoke (myclass, parameter);

}
catch (Exception e)
{
e.printStackTrace ();
}
}
}
above code promised hello world you
This code is equivalent to
MyClass ; myclass = new MyClass ();
myclass.sayHello ("you");
dynamically call can be seen much more complicated than static. Please refer to the specific api function java api
guoliangzhaowen
2010-02-11 08:25:12
attention! Helped UP
liu61111
2010-02-11 08:44:36
One problem: As I said in the comments in the code, and if I do not PrintJavaClassHello mandatory conversion, but also the method call PrintJavaClassHello printHello () do? If this does not, then in what kind of solutions can achieve this it?

use rtti! Detailed look at thinking in java 2nd Chapter 11!
in Object _obj = Class.forName (...) "). newInstance (); obtained after
object called isInstance (), determine whether the resulting obj can perform an action object.
copy thinking in java examples:
/ /: PetCount3.java
/ / Using Java 1.1 isInstance ()
package c11.petcount3;
import java.util. *;

class Pet {}
class Dog extends Pet {}
class Pug extends Dog {}
class Cat extends Pet {}
class Rodent extends Pet {}
class Gerbil extends Rodent {}
class Hamster extends Rodent {}

class Counter {int i;}

public class PetCount3 {
public static void main (String [] args) {
Vector pets = new Vector ( );
Class [] petTypes = {
Pet.class,
; Dog.class,
Pug.class,
; Cat.class,
Rodent.class,
Gerbil.class,
; Hamster.class,
};
try {
for (int i = 0; i <15; i + +) {
/ / Offset by one to eliminate Pet.class:
int rnd = 1 + (int) (
Math.random () * (petTypes.length ; - 1));
pets.addElement (
petTypes [rnd]. newInstance ());
}
} catch ( InstantiationException e) {}
catch (IllegalAccessException e) {}
Hashtable h = new Hashtable ();
for (int i = 0; i <petTypes.length; i + +)
h.put (petTypes [i]. toString (),
new Counter ());
for (int i = 0; i <pets.size (); i + +) {
Object o = pets.elementAt (i);
/ / Using isInstance to eliminate individual
/ / instanceof expressions:
for ( int j = 0; j <petTypes.length; + + j)
if ( petTypes [j]. isInstance (o)) {
String key = petTypes [j]. toString ();
((Counter) h.get (key)). i + +;
;}
}
for (int i = 0; i <pets.size (); i + +)
System.out.println (
pets.elementAt (i). getClass (). toString ());
Enumeration keys = h.keys ();
while (keys.hasMoreElements ()) {
String nm = (String) keys. nextElement ();
Counter cnt = (Counter) h.get (nm);
; System.out.println (
nm.substring (nm.lastIndexOf ('.') + 1) +
"quantity:" + cnt.i);
;}
}
} / / /: ~

Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.