具体详细看:https://blog.csdn.net/qq_36226453/article/details/82790375
目前代码:
package Text; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class RunFanShe { private static final char[] Arcing = null; public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, Exception { Class<?> forName = Class.forName("Text.Arcing"); Object newInstance = forName.getConstructor(String.class).newInstance("刘"); Method methods = forName.getMethod("name", int.class); methods.invoke(newInstance, 20); } } class Arcing{ public String nameString = "bookfar"; public void name(int age) { System.out.println("my name is "+nameString+age); } public Arcing(String aString) { // TODO Auto-generated constructor stub this.nameString = aString; System.out.println("you got it"); } }
交互例子
package SocketProcess; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class SocketClient { public static String readLine="hello"; public static String[] command = {"get", "put"}; @SuppressWarnings("resource") public static void main(String[] args) throws UnknownHostException, IOException { Socket socket = new Socket("localhost", 8899); BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in)); BufferedReader socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter socketOut = new PrintWriter(socket.getOutputStream()); System.out.print("Client:"); readLine = systemIn.readLine(); socketOut.println("get"); socketOut.flush(); String inTempString = socketIn.readLine(); System.out.println(inTempString); System.out.println("Server"); } } class SendMQ{ public String get() { return "you get"; } public String post() { return "you post"; } }
package SocketProcess; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Method; import java.net.ServerSocket; import java.net.Socket; public class SocketServer { public static String[] command; public static String readLine="hello"; public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8899); System.out.println("wite a connect"); Socket accept = serverSocket.accept(); //获取服务器输入 inputStreamReader是字节流转换成字符流 BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in)); BufferedReader sockerIn = new BufferedReader(new InputStreamReader(accept.getInputStream())); //printwriter具有自动行刷新的缓冲字符输出流,特点是可以按行写出字符串,并且可以自动行刷新。 PrintWriter socketOut = new PrintWriter(accept.getOutputStream()); String inTempString = sockerIn.readLine(); System.out.print("Client:"); System.out.println(inTempString); SendMQ sendMQ = new SendMQ(); //将Socket发送过来的字符串直接通过反射方法来操作达到交互 注意invoke和getmethod的工作原理,invoke是需要传入实力化参数的, Object re = sendMQ.getClass().getMethod(inTempString).invoke(sendMQ, args); System.out.println(re); System.out.print("Server >>:"); readLine = systemIn.readLine(); socketOut.println(readLine); socketOut.flush(); } }