怎么在这个程序中加入泛型机制
import java.lang.reflect.*;
import java.io.*;
import java.util.Properties;
public class Account{
private String userName;
private String password;
public Account(String userName,String password){
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
public String getUserName(){
return userName;
}
public void setUesrName(String uesrname){
this.userName=userName;
}
public static Object createObject(String className,String userName,String password){
Object object=null;
try{
Class c1=Class.forName(className);
Class[] typeClass=new Class[2];
typeClass[0]=java.lang.String.class;
typeClass[1]=java.lang.String.class;
java.lang.reflect.Constructor constructor=c1.getConstructor(typeClass);
Object[] inObjects=new Object[2];
object=constructor.newInstance(inObjects);
Class[] parClasses=new Class[1];
parClasses[0]=java.lang.String.class;
Method method=c1.getMethod("setUserName",parClasses);
Object[] objects=new Object[1];
objects[0]=userName;
method.invoke(object,objects);
Class[] parClasses2=new Class[1];
parClasses2[0]=java.lang.String.class;
Method method2=c1.getMethod("setPassword",parClasses2);
Object[] objects2=new Object[1];
objects2[0]=password;
method2.invoke(object,objects2);
}
catch (Exception e){
e.printStackTrace();
}
return object;
}
public static void main(String[] args){
try{
Properties p=new Properties();
p.load(new FileInputStream("class.properties"));
String className=p.getProperty("className");
String userName=p.getProperty("userName");
String password=p.getProperty("password");
Account account=(Account)createObject(className,userName,password);
System.out.println(account.getUserName());
System.out.println(account.getPassword());
}catch(Exception e){
e.printStackTrace();
}
}
}
我做的是一个反射的程序,没加入泛型编译不能通过,请各位高手帮帮忙?
--------------------编程问答-------------------- 沒聽說過不加泛型不能編譯通過的程序 --------------------编程问答-------------------- 上面的代码没有编译不通过的地方呀 --------------------编程问答-------------------- 说错了 是编译时出现了警告 我想把警告消除 不知道怎么弄 --------------------编程问答-------------------- 在你的方法上加上
@SuppressWarnings("unchecked")
注解就可以了 --------------------编程问答-------------------- 那是Class类报的警告吧?
这种和泛型相关的警告无所谓
@SuppressWarnings("rawtypes") 掉吧 --------------------编程问答--------------------
class Account{
private String userName;
private String password;
public Account(String userName,String password){
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
public String getUserName(){
return userName;
}
public void setUesrName(String userName){
this.userName=userName;
}
public static Object createObject(String className,String userName,String password){
Object object=null;
try{
Class<?> c1=Class.forName(className);
Class<?>[] typeClass=new Class[2];
typeClass[0]=java.lang.String.class;
typeClass[1]=java.lang.String.class;
java.lang.reflect.Constructor<?> constructor=c1.getConstructor(typeClass);
Object[] inObjects=new Object[2];
object=constructor.newInstance(inObjects);
Class<?>[] parClasses=new Class[1];
parClasses[0]=java.lang.String.class;
Method method=c1.getMethod("setUserName",parClasses);
Object[] objects=new Object[1];
objects[0]=userName;
method.invoke(object,objects);
Class<?>[] parClasses2=new Class[1];
parClasses2[0]=java.lang.String.class;
Method method2=c1.getMethod("setPassword",parClasses2);
Object[] objects2=new Object[1];
objects2[0]=password;
method2.invoke(object,objects2);
}
catch (Exception e){
e.printStackTrace();
}
return object;
}
public static void main(String[] args){
try{
Properties p=new Properties();
p.load(new FileInputStream("class.properties"));
String className=p.getProperty("className");
String userName=p.getProperty("userName");
String password=p.getProperty("password");
Account account=(Account)createObject(className,userName,password);
System.out.println(account.getUserName());
System.out.println(account.getPassword());
}catch(Exception e){
e.printStackTrace();
}
}
}
补充:Java , Java SE