zoukankan      html  css  js  c++  java
  • The Reflect Part

    The reflect part has many advantages.I heard that reflect is widely used in many frames.

    In this part:

      1.Common method to get Class object:

        a)Class clazz = Class.forName(Qualified Name);

        b)Class clazz = Person.getClass();

        c)Class clazz = Person.class;

      2.Get the method:

        a)Method[] methods = clazz.getDeclaredMethods();

        b)Method method = clazz.getDeclaredMethod(method's name, parameters' class);

      3.Get the constructor:

        a)Constructor[] cons = clazz.getDeclaredConstructors();

        b)Constructor con = clazz.getDeclaredConstructor(parameters' class);

      4.Get the object:

        a)Person person = (Person) clazz.newInstance();

        b)Person person = (Person) constructor.newInstrance(parameters);

      5.Call the method:

        method.invoke(object,parameters);

    What's going on when use the reflect?

      1.Load the person.class

      2.Create space in the heap memory

      3.Initialize the fields of this object

      4.Display the initialization

      5.Initialize the constructor

    If you can use config file to configure the interface or class path, the code's extension will be very convenient.

    For exmaple:

      If these is a file called config.txt. And the config.txt include the class or interface's path from the first line.

      main{

      BufferedReader reader = new BufferedReader(new FileReader(config file path));

      String line = reader.readLine();

      Class clazz = Class.forName(line);

      MyInterface mi = (MyInterface) clazz.newInstance();

      mi.test();

      }

    If you want to use another Class object you can modify the config.txt , that is not a bad choice.

  • 相关阅读:
    Python 语言规范(Google)
    Python 代码风格规范(Google)
    GBM,XGBoost,LightGBM
    面试编程总结
    MagicNotes:如何迈向工作的坦途
    番茄工作法:让时间变成你最好的朋友
    时间管理:如何高效地利用时间
    读点大脑科学,学会变得更聪明
    为什么我那么努力,吃了那么多苦,也没见那么优秀?(转自知乎)
    不要被懒惰夺走你的思考能力
  • 原文地址:https://www.cnblogs.com/ppcoder/p/7127372.html
Copyright © 2011-2022 走看看