zoukankan      html  css  js  c++  java
  • 十:反射

    具体详细看: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");
        }
        
        
    }
    View Code

    交互例子

    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";
        }
        
    }
    socket与反射客户端
    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();
            }
            
        }
    socket与反射服务器
  • 相关阅读:
    显示文件本地文件夹
    Select Dependencies选择依赖项
    搜索小技巧
    783. Minimum Distance Between BST Nodes BST节点之间的最小距离
    5. Longest Palindromic Substring 最长的回文子串
    12. Integer to Roman 整数转罗马数字
    3. Longest Substring Without Repeating Characters 最长的子串不重复字符
    539. Minimum Time Difference 最小时差
    43. Multiply Strings 字符串相乘
    445. Add Two Numbers II 两个数字相加2
  • 原文地址:https://www.cnblogs.com/BookMiki/p/14087766.html
Copyright © 2011-2022 走看看