首页 > Web开发 > 详细

javeweb学习笔记二

时间:2018-04-15 22:46:52      阅读:187      评论:0      收藏:0      [点我收藏+]
1、通过反射获取属性:
class类:

public class Student {

        public Student( ){
        System.out.println("public Student()");
    }

    private void haha(){
        System.out.println("private void haha()");
    }

}

配置文件

class.properties

className=Day01.Student
methodName=haha

获取方法

//加载属性文件,取得类名的方法名
  Properties props = new Properties();
  InputStream is = new FileInputStream("src/Day01/class.properties");
props.load(is);

  String className = props.getProperty("className").trim();
  String methodName = props.getProperty("methodName").trim();

  //通过反射,执行该类的方法
  Class c = Class.forName(className);
  Constructor con = c.getConstructor(null);
  Method m = c.getDeclaredMethod(methodName,null);
m.setAccessible(true); //方式属性是私有 ,如果是私有必须加上
m.invoke(con.newInstance(null),null);

结果:

public Student()
private void haha()

javeweb学习笔记二

原文:http://blog.51cto.com/357712148/2103723

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!