问题
I am having an issue with the toString
method in Java
. Here's the run down... I have a database that I am querying. The query returns all of the members in the database, and I'd like to return these members as a string. Here's what I've done so far.
In the Member class
I have the following
@Override
public String toString()
{
return this.login_name + " (" + this.lastName + ", " + this.firstName + ")";
}
Now, to get the members from the database and return them as an array of strings, here's what I have:
/**
* Accessor used to get all of the user names contained in the underlying
* database.
* @return String[] user_names - The names of all the users in the data base.
* @throws MemberNotFoundSignal
*/
public String[] getMembers() throws MemberNotFoundSignal
{
List<Member> members = Arrays.asList(this.rscMgr.getAllMembers());
String[] user_names = new String[members.size()];
for (int i = 0; i < members.size(); i++)
{
user_names[i] = members.get(i).toString();
}
return user_names;
}
At run time members.get(i).toString()
should invoke the toString method from the Member class (am I mistaken)? If I'm not, why would the objects toString method be invoked on this? It's my understanding that by converting the array to a List, it will utilize the Member's toString method (because of generics).
I'm using this code as follows:
String[] user_names = this.model.getMembers();
this.memberInput = new JComboBox(user_names);
When I set a breakpoint on the getMembers
function the 'user_names' doesn't contain the overriden version of the string that I expect, but instead the objects default return type.
The members.get(i).toString();
doesn't call upon the overridden toString method, but instead calls upon the objects implementation.
回答1:
Based on our chat, we determined that your JAR artifact was not being updated properly. We determined that Member
was being loaded from the correct location by evaluating the following in your debugger:
Member.class.getProtectionDomain().getCodeSource()
But when using reflection to inspect the methods declared by Member
, we could see that the toString()
method was not present:
Member.class.getDeclaredMethods()
And thus we theorized that the JAR was out of date, which you confirmed by inspecting the timestamp of the JAR at your deployment site.
回答2:
You don't need to have the ArrayList to call the toString()
of the member objects. You can call the toString()
while iterating the array. Take a look at the following.
public String[] getMembers(){
Member members = this.rscMgr.getAllMembers();
String userNames[] = new String[members.length];
for(int i=0; i<members.length; i++){
usersNames[i] = member[i].toString();
}
}
回答3:
At run time members.get(i).toString() should invoke the toString method from the Member >class (am I mistaken)? If I'm not, why would the objects toString method be invoked on this? >It's my understanding that by converting the array to a List, it will utilize the Member's >toString method (because of generics).
Remember that generics are compile time only and implemented by erasure. You cannot use it to cast objects. I am guessing your rscMgr.getAllMembers()
returns something other than Member
. Try something like:
int count=0;
for (Member m: rsc.getAllMembers()){
usersNames[count++] = m.toString();
}
来源:https://stackoverflow.com/questions/20053858/java-tostring-issue