黑马程序员技术交流社区

标题: 我发现我也有强迫症了,用Eclip开发程序有黄色标记如何去 [打印本页]

作者: 梦里听传说    时间: 2014-9-18 00:56
标题: 我发现我也有强迫症了,用Eclip开发程序有黄色标记如何去
//强迫症出现在反射阶段 ,其他都搞定了 就一个地方搞定不了,抓狂了。。。!

------------------------------------上代码
package Demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.Set;

/**
* 8、 已知一个类,定义如下:
   package cn.itcast.heima;
      public class DemoClass {
      public void run()
      {
         System.out.println("welcome to heima!");
      }   
     }
     (1) 写一个Properties格式的配置文件,配置类的完整名称。
(2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,用反射 的方式运行run方法。
* @author Administrator
*
*/
public class DayFirst08 {

        public static void main(String[] args) {
               
                config("className1=Demo.DemoClass");
                relectMethod("run");
        }
        //创建Properties格式的配置文件并配置类的完整名称
        public static void config(String className)
        {
                //创建Properties格式的配置文件
                File f=new File("Properties");
                if(!(f.exists()))
                {
                        try {
                                f.createNewFile();
                        } catch (IOException e) {
                                throw new RuntimeException("创建配置文件失败");
                        }
                }
                //配置类的完整名称
                FileWriter xie=null;
                try {
                        xie=new FileWriter(f);
                        xie.write(className);
                       
                } catch (IOException e) {
                        throw new RuntimeException("写入失败");
                }
                finally
                {
                        try {
                                xie.close();
                        } catch (IOException e) {
                                throw new RuntimeException("关闭失败");
                        }
                }
       
        }
        public static void relectMethod(String methodName)
        {
                Properties pro=new Properties();
                try {
                        FileInputStream f=new FileInputStream(new File("Properties"));
                        try {
                                pro.load(f);
                                Set<String> key=pro.stringPropertyNames();
                                for(String name:key)
                                {
                                        String className=pro.getProperty(name);
                                        relectMe(className,methodName);
                                }
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                }
               
               
        }
       
        private static void relectMe(String className,String methodName)
        {
                try {
                        Method m=Class.forName(className).getMethod(methodName, null);//黄色标记在此,求方法去除
                        m.invoke(Class.forName(className).newInstance(), new Object[0]);
                } catch (Exception e) {
                        e.printStackTrace();
       
                        }

        }
}





作者: 289623337    时间: 2014-9-18 05:53
哎,我的Eclipse软件到现在电脑里还运行不了呢???
作者: 梦里听传说    时间: 2014-9-18 11:20
你没配置好吧
作者: 敏敏好学    时间: 2014-9-18 12:26
黄色只是不规范,不影响运行的
作者: 小布袋    时间: 2014-9-18 16:56
黄色一般是警示的,不影响程序的正常运行。
作者: longshaocn    时间: 2014-9-18 20:20
习惯就好,强迫症,应该换个强迫症头像天天看,就好了!
作者: luofeng    时间: 2014-9-18 21:18
我也有点强迫症。
作者: rehan    时间: 2015-2-23 11:12
乱七八糟。。。。代码可读性非常差
作者: cishengbuhuan    时间: 2015-3-10 14:59
一定要要把问题弄好是好习惯啊




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2