定义了这样一些相似的类(动态加载),如何获取类的数组长度?或者指定下标的值?
public class FilterSet_1 {  public static String[] eventType = null;
  public static String[] domain = null;
  public static String[] eventLevel = null;
  public static String[] neType = null;  static {
    eventType = new String[]{
    "BSUpload"
};  domain = new String[]{
    "CONF",
    "HWI",
    "PM",
    "RT"
};    eventLevel = new String[]{};    neType = new String[]{
    "BS",
    "Configuration Session",
    "WAC",
    "WACCL"
};
}比如,我想知道该类的neType[0]的值:BS。

解决方案 »

  1.   

    System.out.println(fieldset.neType[0])
      

  2.   

    老兄你那个static写的是有问题的 你如果要修饰属性 就应该去掉那对{},修饰属性方法: static void f() { eventType = new String[]{ "BSUpload"};} 。估计你这里是修饰方法了,因为你上面已经定义了static String[] eventType 
    以下代码给你参考,没有问题的
    public class FilterSet_1 {   public static String[] eventType = null;
      public static String[] domain = null;
      public static String[] eventLevel = null;
      public static String[] neType = null;   
      public static void main(String args[])
      {
       
    domain = new String[]{"CONF","HWI", "PM","RT"}; eventLevel = new String[]{};    neType = new String[]{"BS", "Configuration Session","WAC","WACCL"};
        //打印数组
        System.out.println(neType.length);
       //打印数组里指定索引的元素 ,索引从0开始
        System.out.println(neType[0]);
        
       }
     static void f(){
      eventType = new String[]{ "BSUpload"};
      }
     
    }
      

  3.   

    看来大家都理解错了,
    1,YidingHe(机枪兵))这些类有好多个,编译时产生,FilterSet_1, FilterSet_2,FilterSet_3, 只有运行时我才知道调用那个。public static void getEventLevel() {
    try {
         Class id = Class.forName("xxx.xxx.xxx.FilterSet_"+prjName);      Field ob = null;
         try{
    ob = id.getField("eventLevel");
             //这里只得到字段名称,不知道如何调用起值。
         }
         catch (NoSuchFieldException e) {
    e.printStackTrace();     
         }
        }
        catch(ClassNotFoundException e){
    e.printStackTrace();
        }
    }2,zhouquan1856() )这里是静态块,里面声明了很多静态数据(数组),不是方法。
      

  4.   

    老兄你那个static写的是有问题的 你如果要修饰属性 就应该去掉那对{},修饰属性方法: static void f() { eventType = new String[]{ "BSUpload"};} 。估计你这里是修饰方法了,因为你上面已经定义了static String[] eventType 
    ---------------------------------------------------------这里是静态执行多个语句的写法,搂住没错。一般反编译里面这种东西比较多,哈哈。
    楼主的问题不是很清楚什么意思,也许Class的下面这个方法你用得上getMethod(String name, Class[] parameterTypes) 
      

  5.   

    或者你这里可以考虑用多态,FACTORY模式来处理,不一定非要用反射,反射虽然是个好东西,但是程序执行效率比较差,可读性差。
      

  6.   

    在jdk1.4中,反射比interface慢40倍
      

  7.   

    反射用Array这个包装类来操作数组。