zoukankan      html  css  js  c++  java
  • 通过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());
        }
    }

  • 相关阅读:
    解决myeclipse2014 中使用低版本的maven插件
    菜鸟成长之路-------使用过滤器实现自动登录
    动态代理
    JSON资料整理
    转账案例中引入事务
    ThreadLocal来管理事务
    【临窗旋墨-leetcode】0001-两数之和-[简单]
    shiro是如何清除过期session的(源码版本shiro1.6)
    [临窗旋墨]javaMelody初始化以及销毁时的处理逻辑及监控日志丢失问题排查
    Eclipse 的 git 插件操作 "代码提交"以及"代码冲突"
  • 原文地址:https://www.cnblogs.com/vonk/p/3954148.html
Copyright © 2011-2022 走看看