我需要从.display()函数中获取对象名称,例如“faruq unique id is blabla”..是否可能?是否有任何方法可以实现它?
public class Login {
String uniqueID;
public String getUniqueID() {
return uniqueID;
}
public void setUniqueID(String activationLink) {
// while(true)
// {
this.uniqueID = activationLink + (int) (Math.random() * 1000);
// we can check in DB whether there already exist this ID.
// if yes,loop until its unique; else break
// break;
// }
}
public void setUniqueID(int activationLink) {
String changed = "" + activationLink;
setUniqueID(changed);
}
void display() {
System.out.println( /* object.getName() */ this.getClass() + " unique id is " + uniqueID);
}
}
这是调用Real的类: -
public class TestLogin {
public static void main(String[] args) {
Login faruq = new Login();
Login prem = new Login();
faruq.setUniqueID(341342);
prem.setUniqueID("xYwkas");
faruq.display();
prem.display();
}
}