Java编译错误:Cannot reduce the visibility of the inherited method from <class>

本文详细解释了Java类继承中的访问权限规则,即在子类中如何正确地重新声明父类的方法以保持或扩大其作用域,同时避免违反面向对象的设计原则。

原因:java类的继承关系中,只能扩大一个函数的作用域范围,不能缩小。

解决方法:在子类中保持父类的作用域范围,或者扩大范围。

 

1.      父类中private的成员函数,可以在子类中重新声明为private, (default), protected, public

2.      父类中(default)的成员函数,可以在子类中重新声明为(default), protected, public

3.      父类中protected的成员函数,可以在子类中重新声明为protected, public

4.      父类中public的成员函数,可以在子类中重新声明为public

 

不能缩小限定其实很好理解,试想一个类的函数已经对外声称具有某种功能了,作为子类按照面向对象的原则,也必须具有这种功能,不能此功能被限定没有了,所以函数的作用域只能扩大而不能缩小,否则是违背面向对象原则的。

 

修改后的代码有以下问题 1:mainApp.switchToHomeScene(); // 假设存在返回主页的方法中,mainApp cannot be resolved 2: private void showErrorAlert(String message)中,Cannot reduce the visibility of the inherited method from Controller 3:private void showInfoAlert(String message)中,private void showInfoAlert(String message) Controller类代码为:package opera; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.stage.Stage; import java.io.IOException; import javafx.event.ActionEvent; // 添加必要的导入 public class Controller { protected Stage getCurrentStage(Parent root) { return (Stage) root.getScene().getWindow(); } protected void showErrorAlert(String message) { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("错误"); alert.setHeaderText(null); alert.setContentText(message); alert.showAndWait(); } protected void showInfoAlert(String message) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("提示"); alert.setHeaderText(null); alert.setContentText(message); alert.showAndWait(); } protected void switchScene(String fxmlPath) throws IOException { Parent root = FXMLLoader.load(getClass().getResource(fxmlPath)); Stage stage = getCurrentStage(root); stage.setScene(new Scene(root)); stage.show(); } // 添加缺失的事件处理方法 public void handleUserLogin(ActionEvent event) { showInfoAlert("用户登录功能"); // 这里添加实际登录逻辑 } public void handleManagerLogin(ActionEvent event) { showInfoAlert("管理员登录功能"); // 这里添加实际登录逻辑 } public void handleExit(ActionEvent event) { Stage stage = (Stage) ((javafx.scene.control.Button) event.getSource()).getScene().getWindow(); stage.close(); } }
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值