zoukankan      html  css  js  c++  java
  • 反射使用案例(2)

    package com.test.reflect;

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.Properties;

    public class Reflect01 {

      public static void main(String[] args) throws Exception {

        School s = new School();
        s.build();

        // s.getComplete(new People());
        // s.getComplete(new Equipment());

        Properties pro = new Properties();
        // FileInputStream in = new FileInputStream("E:\workspace\reflect\bin\complete.properties");
        // InputStream in = new BufferedInputStream (new FileInputStream("E:\workspace\reflect\bin\complete.properties"));
        // InputStream in = Reflect01.class.getResourceAsStream("/complete.properties");
        InputStream in = Reflect01.class.getClassLoader().getResource("\complete.properties").openStream();
        pro.load(in);
        for(int i = 0;i < pro.size();i++) {
          String className = pro.getProperty("complete"+(i+1));
          Class<?> clazz = Class.forName(className);
          Complete c = null;
          if(className.contains("People")) {
            c = (People)clazz.newInstance();
            s.getComplete(c);
          }
          if(className.contains("Equipment")) {
            c = (Equipment)clazz.newInstance();
            s.getComplete(c);
          }

        }
      }

    }

    package com.test.reflect;

    public class School {

      public void build() {
        System.out.println("school ok!");
      }

      public void getComplete(Complete c) {
        if(c != null) {
          c.findComplete();
        }
      }
    }

    package com.test.reflect;

    public interface Complete {

      public void findComplete();
    }

    package com.test.reflect;

    public class People implements Complete{

      @Override
      public void findComplete() {
        this.findTeacher();
        enrollStudents();
      }

      public void findTeacher() {
        System.out.println("Here comes the teacher!");
      }

      public void enrollStudents() {
        System.out.println("many students!");
      }
    }

    package com.test.reflect;

    public class Equipment implements Complete{

      @Override
      public void findComplete() {
        buildHall();
        buildStadium();
      }

      public void buildStadium() {
        System.out.println("stadium ok!");
      }

      public void buildHall() {
        System.out.println("have a beautiful hall!");
      }


    }

    *complete.properties

    complete1=com.test.reflect.People
    complete2=com.test.reflect.Equipment

  • 相关阅读:
    剖析C语言中a=a+++++a的无聊问题
    [转]精确到1%秒的单片机计时器汇编程序
    [转]学DSP、FPGA、ARM,哪个更有前途?
    【Java】Eclipse导出JAR包
    二维码生成器(支持历史记录点击和清空)
    移动端开发注意之一二
    localStorage实现按钮点击禁用
    JavaScript之查找元素
    扒拉扒拉table
    解惑之JavaScript
  • 原文地址:https://www.cnblogs.com/zzlcome/p/11075030.html
Copyright © 2011-2022 走看看