Java中的equals方法

时间:2018-10-06 22:55:10

标签: java override equals

让我们考虑以下简单的类。

00:41:11: Executing task 'run'...


> Task :compileJava FAILED
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\actions\MenuActions.java:5: error: package javafx.scene.control does not exist
import javafx.scene.control.Alert;
                           ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\actions\MenuActions.java:7: error: package javafx.scene.image does not exist
import javafx.scene.image.Image;
                         ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\actions\MenuActions.java:8: error: package javafx.scene.image does not exist
import javafx.scene.image.ImageView;
                         ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:7: error: package javafx.fxml does not exist
import javafx.fxml.FXML;
                  ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:8: error: package javafx.scene.control does not exist
import javafx.scene.control.Button;
                           ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:9: error: package javafx.scene.control does not exist
import javafx.scene.control.Label;
                           ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:10: error: package javafx.scene.control does not exist
import javafx.scene.control.TextInputDialog;
                           ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:27: error: cannot find symbol
    private Label label;
            ^
  symbol:   class Label
  location: class PrimaryController
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:30: error: cannot find symbol
    private Button button;
            ^
  symbol:   class Button
  location: class PrimaryController
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\SecondaryController.java:6: error: package javafx.fxml does not exist
import javafx.fxml.FXML;
                  ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\SecondaryController.java:7: error: package javafx.scene.control does not exist
import javafx.scene.control.Button;
                           ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\SecondaryController.java:20: error: cannot find symbol
    private Button button;
            ^
  symbol:   class Button
  location: class SecondaryController
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\GluonApplication.java:4: error: package javafx.scene does not exist
import javafx.scene.Scene;
                   ^
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\GluonApplication.java:7: error: cannot access Application
public class GluonApplication extends ParticleApplication {
       ^
  class file for javafx.application.Application not found
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\GluonApplication.java:14: error: cannot find symbol
    public void postInit(Scene scene) {
                         ^
  symbol:   class Scene
  location: class GluonApplication
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:26: error: cannot find symbol
    @FXML
     ^
  symbol:   class FXML
  location: class PrimaryController
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:29: error: cannot find symbol
    @FXML
     ^
  symbol:   class FXML
  location: class PrimaryController
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:32: error: cannot find symbol
    @FXML
     ^
  symbol:   class FXML
  location: class PrimaryController
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\SecondaryController.java:19: error: cannot find symbol
    @FXML
     ^
  symbol:   class FXML
  location: class SecondaryController
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\SecondaryController.java:22: error: cannot find symbol
    @FXML
     ^
  symbol:   class FXML
  location: class SecondaryController
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\actions\MenuActions.java:24: error: cannot find symbol
        Alert alert = new Alert(AlertType.INFORMATION);
        ^
  symbol:   class Alert
  location: class MenuActions
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\actions\MenuActions.java:24: error: cannot find symbol
        Alert alert = new Alert(AlertType.INFORMATION);
                          ^
  symbol:   class Alert
  location: class MenuActions
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\actions\MenuActions.java:24: error: cannot find symbol
        Alert alert = new Alert(AlertType.INFORMATION);
                                ^
  symbol:   variable AlertType
  location: class MenuActions
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\actions\MenuActions.java:27: error: cannot find symbol
        alert.setGraphic(new ImageView(new Image(MenuActions.class.getResource("/icon.png").toExternalForm(), 48, 48, true, true)));
                             ^
  symbol:   class ImageView
  location: class MenuActions
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\actions\MenuActions.java:27: error: cannot find symbol
        alert.setGraphic(new ImageView(new Image(MenuActions.class.getResource("/icon.png").toExternalForm(), 48, 48, true, true)));
                                           ^
  symbol:   class Image
  location: class MenuActions
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:51: error: cannot access StackPane
        app.getParticle().getToolBarActions().add(0, actionSignin);
                         ^
  class file for javafx.scene.layout.StackPane not found
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:55: error: cannot access ObservableList
        app.getParticle().getToolBarActions().remove(actionSignin);
                                           ^
  class file for javafx.collections.ObservableList not found
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:65: error: cannot find symbol
        TextInputDialog input = new TextInputDialog(stateManager.getProperty("UserName").orElse("").toString());
        ^
  symbol:   class TextInputDialog
  location: class PrimaryController
E:\IntellijProjects\GluonDesktop-MultipleViewProjectwithFXML\src\main\java\com\gluonapplication\controllers\PrimaryController.java:65: error: cannot find symbol
        TextInputDialog input = new TextInputDialog(stateManager.getProperty("UserName").orElse("").toString());
                                    ^
  symbol:   class TextInputDialog
  location: class PrimaryController
29 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s
1 actionable task: 1 executed
Compilation failed; see the compiler error output for details.
00:41:11: Task execution finished 'run'.

问题在于方法的重写:我使用通用的Object类作为属性来使其更灵活,但是netbeans在返回行上打印错误:“对象没有方法getX”是完全合乎逻辑的。

但是问题仍然存在,如何解决这个问题?

预先感谢您:)

1 个答案:

答案 0 :(得分:2)

这很简单,但是您需要强制转换object

@Override
public boolean equals(Object object){
    if (object != null && object.getClass()==Point.class){
        Point p = (Point)object;
        return p.getX()==this.x && p.getY()==this.y;
    }
    else{
        return false;
    }
}

这也很相关:Casting in equals method