简体   繁体   English

如何从传递的对象中取回值?

[英]How do I retrieve values back values from passed object?

I'm trying to insert different data types into linked list. 我正在尝试将不同的数据类型插入链表。 The problem is that after I insert data I don't know how to read values back when I display my data. 问题是在插入数据后,我不知道如何在显示数据时读回值。

So how to retrieve the values in this kind of order: 那么如何以这种顺序检索值:

List (first-->last): {brand1, 11,111}, {brand2, 22,222}
List (last-->first): {brand2, 22,222}, {brand1, 11,111} 

The output I get represent name of the object and then address of the object: 我得到的输出代表对象的名称,然后是对象的地址:

List (first-->last): Milk@1f5205c Milk@1fb069 
List (last-->first): Milk@1fb069 Milk@1f5205c 

This is my object class: 这是我的对象类:

class Milk <T>
{
    String brand;
    double size;
    double price;

    Milk(String a, double b, double c)
    {
        brand = a;
        size = b;
        price = c;
    }
}

You need to override your Milk class's toString method so that it uses a custom version instead of falling back on Object 's implementation. 您需要重写Milk类的toString方法,以便它使用自定义版本,而不是依靠Object的实现。

In your case, this would probably be as easy as throwing this in to your Milk class. 在您的情况下,这可能就像将其放入Milk课程一样容易。

@Override
public String toString() {

    return brand;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM