继承在IF条件中无法识别instanceOf的问题

时间:2014-01-05 16:56:02

标签: java inheritance instanceof bluej

我有以下继承层次结构:Staff(Abstract),HomeWorker(Abstract),Typist。打字员继承自两个抽象类。

我正在实现一个名为Manager的界面(用于学术作业)。我正在实现Manager的类名为Branch。我需要实现的分支方法之一是setEmail,如下所示。

问题是只有打字员才有电子邮件地址,而不是其他工作人员(翻译员和文员)。所有员工都存储在TreeMap中。在尝试调用setEmail方法之前,我正在尝试检查id参数指定的staff成员是instanceOf Typist(因为此方法仅存在于Typist上)。

然而,使用BlueJ,只要我在IF条件中输入instanceOf,范围突出显示就会告诉我它超出了范围,并且在staffMember引用之后需要')'。

任何想法??

/** Sets email for a typist

 * @param id represents the staff id
 * @param email is the email address
 */ 
public void setEmail(String id, String email)
{
    //Should be done in the typist class. This increases coupling though. 
    //Need to see if there is a way that I can do this using the staff class instead 
    Staff staffMember = staff.get (id);
    //TO DO: Need to put an IF statement in that confirms the staff member is a typist,    otherwise I could end up
    //trying to set an email against a staff type which doesn't store email addresses
    if (staffMember instanceOf Typist)
    {
    staffMember.setEmail(email);      
    }

}

1 个答案:

答案 0 :(得分:2)

尝试使用关键字

instanceof

而不是

instanceOf

因为最后一个不是Java中的关键字。