日期:2014-05-20 浏览次数:20781 次
//先封装一个数据封装的类,Player.java package test02; public class Player { private String ID; private String passWord; private String loginMode; public Player(){ } public Player(String id, String passWord, String loginMode) { super(); ID = id; this.passWord = passWord; this.loginMode = loginMode; } public String getID() { return ID; } public void setID(String id) { ID = id; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getLoginMode() { return loginMode; } public void setLoginMode(String loginMode) { this.loginMode = loginMode; } public String toString(){ return this.ID+" " + this.passWord + " " + this.loginMode; } //写一个发送到服务器的信息 public void send(String serverip){ //具体的服务器IP,和数据 String s = this.toString(); //将s转换为服务器所接受的字节码,或者其他编码类型 //根据数据的封装,服务器的IP来发送,我这是根据对象的toString来发送 //假定服务器是通过解析String来执行操作的; } } //通过反射机制来动态加载文件和player.class package test02; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Properties; public class LoginPlayer { private static Properties p; static{ p = new Properties(); try { p.load(new FileInputStream("login.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String loginServerIP = p.getProperty("loginServerIP"); String ID = p.getProperty("ID"); String passWord =p.getProperty("passWord"); String loginMode = p.getProperty("loginMode"); String TypeClass = p.getProperty("TypeClass"); try { Class<?> c = Class.forName("test02.Player"); Class<?> cc = Class.forName(TypeClass); Constructor<?> con = null; try { con = c.getConstructor(cc,cc,cc,cc); Object obj = null; try { obj = con.newInstance(ID,passWord,loginMode); Method m = c.getDeclaredMethod("send", cc); m.invoke(obj,loginServerIP); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }