Java的问题的toString [英] Java toString Issue

查看:98
本文介绍了Java的问题的toString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与的toString 方法的Java 的问题。这里的跑下来......我有我查询数据库。该查询返回数据库中的所有成员,我想这些成员返回一个字符串。这是我到目前为止已经完成。

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.

会员类我有以下

 @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;
}

在运行时 members.get(I)的ToString()应该调用各会员类toString方法(是我错)?如果我没有,为什么会对象toString方法来对这个调用?这是我的理解是由数组转换为一个列表,它会利用会员的toString方法(因为仿制药的)。

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).

我使用这个code如下:

I'm using this code as follows:

String[] user_names = this.model.getMembers();
this.memberInput = new JComboBox(user_names);

当我设置一个断点 getMembers 函数'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.

members.get(I)的ToString(); 在被覆盖的toString方法不叫,但在对象执行,而不是调用

The members.get(i).toString(); doesn't call upon the overridden toString method, but instead calls upon the objects implementation.

推荐答案

根据我们的聊天,我们确定你的JAR神器没有被正确地更新。我们确定成员正在从正确加载的位置的通过评估你的调试器以下内容:

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()

但是,使用反射时,检查由成员声明的方法,我们可以看到的toString()方法没有present:

But when using reflection to inspect the methods declared by Member, we could see that the toString() method was not present:

Member.class.getDeclaredMethods()

和因此,我们推测,这个JAR已经过时,您可以通过在部署现场检查JAR的时间戳证​​实。

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.

这篇关于Java的问题的toString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆