• 通过class实例取得类的接口,父类,构造器


    interface China {
        public static final String NATIONAL = "JAPAN";
        public static final String AUTHOR = "SIGERU";

        public void sayJapan();

        public String sayHello(String name, int age);
    }

    public class PersonZ implements China {
        private String name;
        private int age;

        public PersonZ() {
        }

        public PersonZ(String name, int age) {
            this(name);
            this.setAge(age);
        }

        public PersonZ(String name) {
            super();
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public void sayJapan() {
            System.out.println("AUTHOR:" + AUTHOR + ",COUNTRY:" + NATIONAL);
        }

        @Override
        public String sayHello(String name, int age) {
            return name + ",Hello! I AM " + age + "NOW!";
        }

    }

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Modifier;

    public class GetInterfaceDemo {
        public static void main(String[] args) {
            Class<?> c1 = null;
            String path = PersonZ.class.getName();
            try {
                c1 = Class.forName(path);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Class<?> c[] = c1.getInterfaces();
            Constructor<?> con[] = c1.getConstructors();
            for (int i = 0; i < c.length; i++) {
                System.out
                        .println("THe interface implemented is:" + c[i].getName());
            }
            for (int i = 0; i < con.length; i++) {
                Class<?> p[] = con[i].getParameterTypes();
                System.out.print("The constructor is:"
                        + Modifier.toString(con[i].getModifiers()) + " "     //使用modifier还原修饰符
                        + con[i].getName());
                System.out.print("(");
                for (int j = 0; j < p.length; j++) {
                    System.out.print(p[j].getName() + "arg" + i);
                    if (j < p.length - 1) {
                        System.out.print(",");
                    }
                }
                System.out.println("){}");
            }
            System.out.println("The superclass extended is:"
                    + c1.getSuperclass().getName());
        }
    }

  • 相关阅读:
    Python 调用multiprocessing模块下面的Process类方法(实现服务器、客户端并发)-TCP协议
    Python开启进程的2中方式
    基于UDP的交互的实例
    Python socket粘包问题(最终解决办法)
    Python socket粘包问题(初级解决办法)
    Python socket套字节
    OSI七层模型
    异常处理
    Python封装与隐藏
    螺旋队列问题
  • 原文地址:https://www.cnblogs.com/vonk/p/3954148.html
Copyright © 2020-2023  润新知