在if语句中使用它

时间:2016-04-13 02:40:18

标签: c++ this

所以我意识到import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLayeredPane; import javax.swing.JPanel; /** * @see https://stackoverflow.com/a/36587584/230513 * @see https://stackoverflow.com/a/6432291/230513 * @see https://stackoverflow.com/questions/6432170 */ public class CardPanel extends JLayeredPane { private static final Dimension d = new Dimension(320, 240); private static final Random random = new Random(); private static final JPanel cards = new JPanel(new CardLayout()); private static final JComboBox combo = new JComboBox(); private final String name; public CardPanel(String name) { this.name = name; this.setBackground(new Color(random.nextInt())); this.add(new LayerPanel(1 * d.height / 8), 100); this.add(new LayerPanel(2 * d.height / 8), 101); this.add(new LayerPanel(3 * d.height / 8), 102); } @Override public Dimension getPreferredSize() { return d; } @Override public String toString() { return name; } private static class LayerPanel extends JPanel { private static final Random r = new Random(); private int n; private Color color = new Color(r.nextInt()); public LayerPanel(int n) { this.n = n; this.setOpaque(false); this.setBounds(n, n, d.width / 2, d.height / 2); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(color); g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16); g2d.setColor(Color.black); g2d.drawString(String.valueOf(n), 5, getHeight() - 5); } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { create(); } }); } private static void create() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for (int i = 1; i < 9; i++) { CardPanel p = new CardPanel("Panel " + String.valueOf(i)); combo.addItem(p); cards.add(p, p.toString()); } JPanel control = new JPanel(); combo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JComboBox jcb = (JComboBox) e.getSource(); CardLayout cl = (CardLayout) cards.getLayout(); cl.show(cards, jcb.getSelectedItem().toString()); } }); control.add(combo); f.add(cards, BorderLayout.CENTER); f.add(control, BorderLayout.SOUTH); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } } 是对类本身的引用,但是我无法真正告诉它在if语句中的作用

以下代码有什么作用?

this

我相当确定它是否检查该类是否为空,但进一步解释会很棒!

3 个答案:

答案 0 :(得分:1)

this指针是一个常量指针,用于保存当前对象的内存地址。因此,从技术上讲,这将检查它是否为null,在成员函数中不为null。由于在拥有对象和抽象类之前无法调用类,因此无论如何都无法使用它。所以,这个没有多大意义。

答案 1 :(得分:0)

它正在尝试检查是否在NULL指针上调用了该方法,例如像这样的东西:

Foo * foo = NULL;
foo->TheMethod();

但是,这不是一种有效的技术,因为在NULL指针上调用方法(甚至是非虚方法!)是未定义的行为,因此测试将无法可靠地工作。

答案 2 :(得分:0)

确实在检查this != nullptr。但是,当您达到此声明时,您已经从未定义的行为传递过来。因为this仅在类方法中可用,并且使用nullptr调用此方法是UB。

因此,这种陈述从来没有任何有效的目的。

除了UB之外,还要记住这样的检查只有在用指针调用时才有用。它们与对象/引用无关。如果方法是virtual,则大多数架构会在到达if之前使代码崩溃。