无法在ArrayList中查找对象的方法

时间:2016-05-15 21:02:00

标签: java arraylist subclass superclass

所以我得到错误"找不到符号 - 方法getIsNational()"在课程Ex6中, 这是所涉及的所有类的代码

public class Message
{
     private String sender;
     private String receiver;
     private String content;

     public Message(String ssender, String rreceiver, String ccontent){
         sender=ssender;
         receiver=rreceiver;
         content=ccontent;
     }

     public String getSender(){
         return sender;
     }

     public String getReceiver(){
         return receiver;
     }

     public String getContent(){
         return content;
     }
}

上面是带有一些getter方法的超类Message

public class NationalMessage extends Message
{
    private boolean isNational;
    public NationalMessage(String ssender, String rreceiver, String ccontent){
        super(ssender, rreceiver, ccontent);
        if(ssender.startsWith("UK") && rreceiver.startsWith("UK")){ //If the sender and reciever are from uk then it is national
            isNational = true;
        }else{//if not then it isnt national
            isNational=false;
        }
    }
    public boolean getIsNational(){
        return isNational;
    }
}

上面是子类NationalMessage

import java.util.*;
public class Ex6 {
     public static int countNational (ArrayList<Message> messageList) {
        // This method just adds up the amount of Messages that are of type NationalMessage and when getIsNational is true.
        int sum = 0;
         try{
             for(int i = 0; i < messageList.size(); i++){ //loop through arraylist given
                 if(messageList.get(i) instanceof NationalMessage){//if its of this type continue
                    if((messageList.get(i)).getIsNational()){ //Error occurs here
                        sum += 1;
                    }
                }
                }
             return sum;
        }catch(NullPointerExcpetion e){//in the case of messageList being null
            System.out.println("Error");
            return -1;
        }
     }
     public static void main(String[] args){ // you can use this main method to test your
         ArrayList<Message> messageList = new ArrayList<Message>();
         messageList.add(new NationalMessage("UKJohn","UKMark","aa"));
         messageList.add(new NationalMessage("UKJohn","FRJean","aa"));
         messageList.add(new Message("Mike","John","aa"));
         System.out.println(countNational(messageList));
     }
}

上面的类是我在第9行得到错误的地方,我不明白为什么它找不到方法,我确定它是一个简单的修复,但我该如何解决这个问题? 在此先感谢您的帮助。

5 个答案:

答案 0 :(得分:3)

您必须将Message强制转换为NationalMessageinstanceof检查不足以让您拨打getIsNational()

有关转换语法的详细信息,请参阅Java casting order

答案 1 :(得分:0)

好吧,Message类没有方法getIsNational,您将messageList声明为Message个对象的列表。
最快的解决方法是添加此方法和

throw new UnsupportedOperationException("Not implemented yet");

如果没别的话 但我建议创建一些所有消息类扩展/实现的抽象类或接口。这样,您可以声明接口(或抽象类)列表,并在其中存储从列表中扩展/实现类的类的对象。
当您以这种方式组织代码时,消息类型的“规范”唯一地位于抽象类(或接口)中,并且各种实现可以保存在单独的类中(如示例中的Message和NationalMessage)。

答案 2 :(得分:0)

当您在getIsNational()元素中调用messageList时,编译器会认为该对象的类型为Message。但是Message中没有带有该名称的方法,因此您应该将该元素转换为NationalMessage,如下所示

 if(((NationalMessage)messageList.get(i)).getIsNational()){..

答案 3 :(得分:0)

在方法ex6的方法countNational中:
更新if条件如下:

if(messageList.get(i) instanceof NationalMessage){//if its of this type continue
  final NationalMessage nationalMessage = (NationalMessage) messageList.get(i) // You need to cast the object to NationalMessage after checking that the objectis an instance of NationalMessage
  if(nationalMessage.getIsNational()){ //Error occurs here
    sum += 1;
  }
}

答案 4 :(得分:0)

在你缺少的Message类中也放了一个getIsNational()方法。 NullPointerExcpetion还有一件事吗? :)你需要输入NullPointerException。