gpt4 book ai didi

java - 无法降低对象继承方法的可见性

转载 作者:行者123 更新时间:2023-12-01 11:41:59 25 4
gpt4 key购买 nike

我定义了以下两个类:

public abstract class Subject {

private ArrayList<ClockObserver> clockObserverList = new ArrayList<ClockObserver>();


public void attach(ClockObserver clockObserver) {
// begin-user-code
// TODO Auto-generated method stub
clockObserverList.add(clockObserver);
// end-user-code
}
public void dettach(ClockObserver clockObserver) {
// begin-user-code
// TODO Auto-generated method stub
clockObserverList.remove(clockObserver);
// end-user-code
}

protected void notify() {
// begin-user-code
// TODO Auto-generated method stub
for(int i= 0; i < clockObserverList.size(); i++)
{
clockObserverList.get(i).update();
}
// end-user-code
}
}

public class SystemClock extends Subject {
private int hour;
private int minute;
private int second;
public void setTime(int hour, int minute, int second) {
this.hour = hour;
this.minute= minute;
this.second = second;
notify();
}
public ClockTime getTime() {
ClockTime clockTime = new ClockTime();
clockTime.hour = this.hour;
clockTime.minute = this.minute;
clockTime.second = this.second;
return clockTime;
}
public void displayTime() {

System.out.println("Time is :" + this.hour + ":" + this.minute + ":" + this.second);
}
}

我在通知函数中遇到以下错误:

Multiple markers at this line
- Cannot override the final method from Object
- overrides java.lang.Object.notify
- Cannot reduce the visibility of the inherited method from

即使我将其可见性从 protected 更改为公开,我仍然遇到以下错误:

Multiple markers at this line
- Cannot override the final method from Object

你能帮我看看问题出在哪里吗?

最佳答案

在 Java 中,每个类都隐式扩展 Object 类,该类定义了一个名为 notify 的方法。因此,如果您在类中创建一个 notify 方法,编译器会认为您试图重写 Object.notify 方法,但事实显然并非如此。

只需重命名您的方法 notify 就可以了。

关于java - 无法降低对象继承方法的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29448108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com