【问题标题】:Accessing the methods of a member of an Object[] array访问 Object[] 数组成员的方法
【发布时间】:2016-05-18 15:43:21
【问题描述】:

我必须创建一个对象数组,然后随机填充。在这个数组中,我需要放置 100 个随机数,Person(base) Student(sub),Professor(sub),Course(一个 Student 加上一个 Profes 的数组)和一个 Circle(不相关)。我还必须命名并计算我输入到数组中的每个人(包括教授和学生)。

    Object[] array = new Object[100];
    String[] names = new String[]{"Ben","Anne","Joe","Sue","John","Betty","Robert","Mary",
                                  "Mark","Jane","Paul","Willow","Alex","Courtney","Jack",
                                  "Rachel"};
    int count = 0;
    for(int i=0; i<100; i++){
       int a = (int)(Math.random()*5);
       String n = names[(int)(Math.random()*16)];
       if(a == 0){array[i]= new Person(n); count++;}
       else if(a == 1){array[i]= new Student(n); count++;}
       else if(a == 2){array[i]= new Professor(n); count++;}
       else if(a == 3){
           array[i]= new Course();
           count = count + 11;
           for(int j = 0; j<10; j++){
               String l = names[(int)(Math.random()*16)];
               array[i].getClasslist()[j].setName(l);}
       }
       else if(a == 4){array[i]= new Circle();}
    }

但是,每当我尝试调用其中一个成员的方法时,它都会告诉我“找不到 Symbol-Method getClasslist()”或 setName 或我试图调用的任何内容。知道如何解决这个问题吗?

【问题讨论】:

  • 在没有任何通用接口的情况下将不同类型的对象混合在一个数组中通常表明您的设计中的某些内容确实存在严重问题。
  • 你确定这是你的任务要求你做的吗?

标签: java arrays object cannot-find-symbol


【解决方案1】:

就您的实际代码而言,您需要按照您编写的方式显式转换 array[i]:

for(int j = 0; j<10; j++){
   String l = names[(int)(Math.random()*16)];
   ((Course) array[i]).getClasslist()[j].setName(l);
}

...尽管在一个数组中混合许多不同类型而没有公共接口的整个事情是一种巨大的代码气味。

【讨论】:

    【解决方案2】:

    当您有一系列混合对象时,您必须先检查对象的实际类型,然后才能调用特定于类型的方法。为此,请使用instanceof:

    for (Object obj : array) {
        if (obj instanceof Person) { // includes subclasses Student and Professor
            Person person = (Person)obj;
            // now you can call Person methods
        } else if (obj instanceof Course) {
            Course course = (Course)obj;
            for (Object member : course.getMembers()) {
                if (member instanceof Person) {
                    Person person = (Person)member;
                    // now you can call Person methods
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我认为将所有这些不同的类放在Object[] 中是完全可以的,只要在程序的后面部分中,数组的所有元素都只用作对象(好吧,Object 不是那样一个精美的界面 - 但这只是一个练习)。

      但最好初始化整个Course 对象,然后才将其放入数组中:

         else if(a == 3){
             Course course = new Course();
             count = count + 11;
             for(int j = 0; j<10; j++){
                 String l = names[(int)(Math.random()*16)];
                 course.getClasslist()[j].setName(l);//no casting needed
             }
             array[i]=course;//we can forget the real type of the object now
         }
      

      【讨论】:

      • 我刚回到 stackoverflow 上,意识到我从来没有因为你的回答给你功劳。迟到总比没有好!
      猜你喜欢
      • 2017-08-24
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多