当我在课堂上比较对this
的引用时,它会警告我:
可能的意外参考比较;要进行值比较,请将左侧的左侧键入Object
它是非常简单的代码行:
if (otherTarget != null && otherTarget.TargetComponent == this)
它完全按照我的预期工作,但是此警告存在,我不知道为什么?我可以忽略它,还是在代码逻辑方面做得不好?
答案 0 :(得分:0)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Test {
private JFrame frame;
private JPanel pane;
private JPanel cellsPane;
private MyCell[][] cells;
private JButton button;
private Timer timer;
private int counter = 3;
private boolean isFinished = false;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().createAndShowGui());
}
private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
pane = new JPanel();
cellsPane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
cellsPane.setLayout(new GridLayout(4, 4, 5, 5));
cells = new MyCell[4][4];
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
cells[i][j] = new MyCell(Color.WHITE);
cellsPane.add(cells[i][j]);
}
}
button = new JButton("Press me!");
timer = new Timer(1000, listener);
button.addActionListener(e -> {
button.setEnabled(false);
isFinished = false;
updateCellsColors();
timer.start();
});
pane.add(cellsPane);
pane.add(button);
frame.add(pane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void updateCellsColors() {
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
cells[i][j].setCellColor(isFinished ? Color.WHITE : Color.BLUE);
}
}
}
private ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (counter == 0) {
timer.stop();
counter = 3;
isFinished = true;
button.setEnabled(true);
updateCellsColors();
}
if (isFinished) {
button.setText("Press me!");
} else {
button.setText("You have " + counter + " seconds remaining");
}
counter--;
}
};
}
@SuppressWarnings("serial")
class MyCell extends JPanel {
private Color cellColor;
public Color getCellColor() {
return cellColor;
}
public void setCellColor(Color cellColor) {
this.cellColor = cellColor;
this.setBackground(cellColor);
}
public MyCell(Color cellColor) {
this.cellColor = cellColor;
this.setOpaque(true);
this.setBackground(cellColor);
}
@Override
public Dimension getPreferredSize() {
// TODO Auto-generated method stub
return new Dimension(30, 30);
}
}
(或其祖先)的类型有一个自定义运算符==,但是表达式this
将使用otherTarget.TargetComponent == this
进行引用比较,因此会出现警告。
如果您确实要进行引用比较,请将其强制转换为对象:
operator== (object, object)
如果要使用自定义运算符==,请将左侧对象转换为otherTarget.TargetComponent == (object)this
类型:
this
如果您不确定,请选择禁用:
(TYPE_OF_THIS)otherTarget.TargetComponent == this
or
(otherTarget.TargetComponent as TYPE_OF_THIS) == this