有没有办法通过child class in Java禁用一部分继承的功能?

Is there a way to disable a part of a inherited function through child class in Java?

我正在 Java 中构建一个 GUI 项目。在 parent class 中,我实现了 MouseListener 以在光标悬停在按钮上方时突出显示按钮,并在光标悬停在按钮上方时停止。我想在 child class 中禁用它,这样所选的按钮就会永久突出显示它已被选中。在 child class 中,当我将鼠标悬停在它上面时,它再次改变了颜色。我想从 child class 中删除该函数的特定部分,但我必须重新编写它以覆盖该函数。有什么办法可以在 child class?

中禁用该部分

这是代码片段

public void mouseEntered(MouseEvent e) {
    super.mouseEntered(e);
    if (e.getSource() == notifications) {
        notifications.setBackground(new Color(0x13A89E));
    }
    if (e.getSource() == attendance) {
        attendance.setBackground(new Color(0x13A89E));
    }
    if (e.getSource() == marks) {
        marks.setBackground(new Color(0x13A89E));
    }
    if (e.getSource() == learning) {
        learning.setBackground(new Color(0x13A89E));
    }
    if (e.getSource() == assignments) {
        assignments.setBackground(new Color(0x13A89E));
    }
    if (e.getSource() == GDB) {
        GDB.setBackground(new Color(0x13A89E));
    }
    if (e.getSource() == MDB) {
        MDB.setBackground(new Color(0x13A89E));
    }
    if (e.getSource() == quizzes) {
        quizzes.setBackground(new Color(0x13A89E));
    }
    if (e.getSource() == lectureContents) {
        lectureContents.setBackground(new Color(0x13A89E));
    }
    if (e.getSource() == courseInformation) {
        courseInformation.setBackground(new Color(0x13A89E));
    }
}

public void mouseExited(MouseEvent e) {
    super.mouseExited(e);
    if (e.getSource() == notifications) {
        notifications.setBackground(null);
    }
    if (e.getSource() == attendance) {
        attendance.setBackground(null);
    }
    if (e.getSource() == marks) {
        marks.setBackground(null);
    }
    if (e.getSource() == learning) {
        learning.setBackground(null);
    }
    if (e.getSource() == assignments) {
        assignments.setBackground(null);
    }
    if (e.getSource() == GDB) {
        GDB.setBackground(null);
    }
    if (e.getSource() == MDB) {
        MDB.setBackground(null);
    }
    if (e.getSource() == quizzes) {
        quizzes.setBackground(null);
    }
    if (e.getSource() == lectureContents) {
        lectureContents.setBackground(null);
    }
    if (e.getSource() == courseInformation) {
        courseInformation.setBackground(null);
    }
}

你的 parent class 的方法是 public 我看不出你为什么不能简单地覆盖它们并决定你想做什么:

Parent.java:

public class Parent implements MouseListener {
    @Override  
    public void mouseEntered(MouseEvent e) {
        // all your logic here to highlight when mouse entered/hovered
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // all your logic here for clearing the highlight when the mouse has exited or is not hovering over the component
    }
}

Child.java:

public class Child extends Parent {
    @Override  
    public void mouseEntered(MouseEvent e) {
        // we call super here as we want to inherit logic of the parent class for highlighting on mouse entered/hover
        super.mouseEntered(e);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // we dont call super here as we don't want to inherit the parents behavior
    }
}

我解决了这个问题。在 child class 中,重新引入你不想改变的 属性 方法体,并将其写在 MouseExited 方法的顶部,并将方法体留空。然后在 else 部分调用 super.mouseEntered(e) 函数。这样,条件将在顶部得到满足,并且由于主体是空的,因此不会发生任何变化。 这是代码

Parent Class:

public void mouseEntered(MouseEvent e) {
    if (e.getSource() == dashboard) {
        dashboard.setBackground(new Color(0x13A89E));
        dashboard.setForeground(Color.white);
    }
    else if (e.getSource() == regCard) {
        regCard.setBackground(new Color(0x13A89E));
        regCard.setForeground(Color.white);
    }
    else if (e.getSource() == fees) {
        fees.setBackground(new Color(0x13A89E));
        fees.setForeground(Color.white);
    }
    else if (e.getSource() == resultCard) {
        resultCard.setBackground(new Color(0x13A89E));
        resultCard.setForeground(Color.white);
    }
    else if (e.getSource() == profile) {
        profile.setBackground(new Color(0x13A89E));
        profile.setForeground(Color.white);
    }
    else if (e.getSource() == logOut) {
        logOut.setBackground(new Color(0x13A89E));
        logOut.setForeground(Color.white);
    }
}

@Override
public void mouseExited(MouseEvent e) {
    if (e.getSource() == dashboard) {
        dashboard.setBackground(Color.WHITE);
        dashboard.setForeground(Color.black);
    }
    else if (e.getSource() == regCard) {
        regCard.setBackground(Color.WHITE);
        regCard.setForeground(Color.black);
    }
    else if (e.getSource() == fees) {
        fees.setBackground(Color.WHITE);
        fees.setForeground(Color.black);
    }
    else if (e.getSource() == resultCard) {
        resultCard.setBackground(Color.WHITE);
        resultCard.setForeground(Color.black);
    }
    else if (e.getSource() == profile) {
        profile.setBackground(Color.WHITE);
        profile.setForeground(Color.black);
    }
    else if (e.getSource() == logOut) {
        logOut.setBackground(Color.WHITE);
        logOut.setForeground(Color.black);
    }
}

Child Class:

public void mouseExited(MouseEvent e) {
    if (e.getSource() == dashboard) {
        //Do nothing
    } else {
        super.mouseExited(e);
    }
}