当前位置: 技术问答>java相关
请教:如何动态构建一个类的事例??(今天结账谢谢2002-7-31)
来源: 互联网 发布时间:2015-07-27
本文导语: 要求通过变量传入类名动态构造一个事例,比如: String strClassName = "java.util.vector"; object o = 函数(strClassName); (不要告诉我用if (strClassName.compareTo("java.util.vector")) return new vetor()) 谢谢 | /* * SignIn...
要求通过变量传入类名动态构造一个事例,比如:
String strClassName = "java.util.vector";
object o = 函数(strClassName);
(不要告诉我用if (strClassName.compareTo("java.util.vector")) return new vetor())
谢谢
|
/*
* SignInFactory.java
*/
package com.cxj.User;
/*
* 抽象工厂
* @author cxj
* @date 2002/07/23
* @version 1.0.0
*/
import com.cxj.Mail.Session;
public abstract class SignInFactory {
//note:目前仅仅提供mysql的数据存储
private static String className = "com.cxj.User.MysqlSignIn";
private SignInFactory signInFactory = null;
/*
* 生成className定义的类的实例
* @return SignInFactory 的实例
*/
public synchronized static SignInFactory getInstance() {
if(signInFactory == null) {
try {
Class c = Class.forName(className);
signInFactory = (signInFactory) c.newInstance();
} catch (Exception ex) {
com.cxj.Tool.File.log(ex.toString());
return null;
}
}
return signInFactory;
}
/*
* 返回描述用户信息的Session类
* @param userName 用户名
* @param domains 用户所在的域
* @param passwd 用户密码
* @return Session 描述用户信息
*/
public abstract Session getSession(String userName,String domains,String passwd);
}
* SignInFactory.java
*/
package com.cxj.User;
/*
* 抽象工厂
* @author cxj
* @date 2002/07/23
* @version 1.0.0
*/
import com.cxj.Mail.Session;
public abstract class SignInFactory {
//note:目前仅仅提供mysql的数据存储
private static String className = "com.cxj.User.MysqlSignIn";
private SignInFactory signInFactory = null;
/*
* 生成className定义的类的实例
* @return SignInFactory 的实例
*/
public synchronized static SignInFactory getInstance() {
if(signInFactory == null) {
try {
Class c = Class.forName(className);
signInFactory = (signInFactory) c.newInstance();
} catch (Exception ex) {
com.cxj.Tool.File.log(ex.toString());
return null;
}
}
return signInFactory;
}
/*
* 返回描述用户信息的Session类
* @param userName 用户名
* @param domains 用户所在的域
* @param passwd 用户密码
* @return Session 描述用户信息
*/
public abstract Session getSession(String userName,String domains,String passwd);
}
|
public Object 函数(strClassName)
{
Object obj = null;
try
{
Class cls = Class.forName(strClassName);
obj = cls.newInstance();
}
catch(Exception e){}
return obj;
}
{
Object obj = null;
try
{
Class cls = Class.forName(strClassName);
obj = cls.newInstance();
}
catch(Exception e){}
return obj;
}
|
import java.lang.reflect.*;
public static Object getInstanceByName(String className){
Class cl = Class.forName(className);
return cl.newInstance();
}
//client:
String className = "java.util.Vector";
java.util.Vector vector = (java.util.Vector)getInstanceByName(className);
其他RTTI信息可以用Reflection机制的其他相应类得到,例如Method,Constructor等。
public static Object getInstanceByName(String className){
Class cl = Class.forName(className);
return cl.newInstance();
}
//client:
String className = "java.util.Vector";
java.util.Vector vector = (java.util.Vector)getInstanceByName(className);
其他RTTI信息可以用Reflection机制的其他相应类得到,例如Method,Constructor等。
|
Class.forName(your String);