假设我有以下计划:
int a=7; // while &a=0x67a9
有没有办法改变 a 的地址,为ex。 0xAA55将?
答案 0 :(得分:6)
不,那是不可能的。局部变量的地址是寄存器或堆栈地址。所以这些甚至可以随着每个调用上下文而改变。
如果您知道您的硬件支持在该特定地址处拥有值,您可以使用指针:
public class Grafik extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Grafik frame = new Grafik();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Grafik() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 784, 419);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("Click");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fire();
}
});
btnNewButton.setBounds(64, 73, 32, 32);
contentPane.add(btnNewButton);
}
public void fire()
{
JPanel panel = new JPanel(){
@Override
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawOval(20, 20, 400, 400);
g.fillOval(20,20,400,400);
g.setColor(Color.white);
g.drawOval(60, 60, 300, 300);
g.fillOval(60,60,300,300);
}
};
panel.setBounds(10, 27, 383, 233);
setContentPane(panel);
JButton btn = new JButton();
btn.setIcon(new ImageIcon("C:\\Users\\nuria\\Desktop\\icon1.png"));
btn.setBounds(75, 76, 32, 32);
panel.add(btn);
}
}
并访问值
volatile int* pa = 0x67a9;