在 IF 条件下无法识别实例的继承问题



我有以下继承层次结构:员工(摘要),家庭工作者(摘要),打字员。Typist 继承自这两个抽象类。

我正在实现一个名为管理器的接口(用于学术作业)。我在其中实现管理器的类称为分支。我需要实现的 Branch 中的一种方法是 setEmail,如下所示。

问题是只有打字员有电子邮件地址,没有其他工作人员(翻译和文员)。所有员工都存储在树状图中。在尝试调用 setEmail 方法之前,我正在尝试检查 id 参数指定的工作人员是否是 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);      
    }
}

尝试使用关键字

instanceof

而不是

instanceOf

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

最新更新