我有一个实现ActionListener的类SaveFile。其中的方法采用字符串参数compleName。如何在该类的actionPerformed方法中使completeName可访问。
由于
class SaveFile implements ActionListener {
public void uploadToDatabase(String completeName){
}
public void actionPerformed(ActionEvent e) {
// I want to access completeName here
}
}
}
答案 0 :(得分:3)
只需将其用作课堂内的变量
class SaveFile implements ActionListener {
private string completeName;
public void uploadToDatabase(String compName){
//code...
this.completeName = compName;
}
public void actionPerformed(ActionEvent e) {
System.out.println(completeName);
}
}
}
答案 1 :(得分:2)
只需确保将completeName
存储为实例变量。
class SaveFile implements ActionListener {
private String completeName;
public void uploadToDatabase(String completeName){
// do other things
this.completeName = completeName;
}
public void actionPerformed(ActionEvent e) {
// use this.completeName to get that value
}
}
}