简体   繁体   English

如何将@Override toString与接口对象的ArrayList一起使用?

[英]How do I use @Override toString with an ArrayList of interface objects?

My understanding of interfaces is that they cannot contain method definitions, only signatures and constant declarations. 我对接口的理解是它们不能包含方法定义,只能包含签名和常量声明。 I have an arraylist that contains a collection of interface objects. 我有一个包含接口对象集合的arraylist。 In this case, it looks like: 在这种情况下,它看起来像:

List< StoreItem > items = new ArrayList< StoreItem >(); List <StoreItem> items = new ArrayList <StoreItem>();

and StoreItem is my interface that looks like this: StoreItem是我的界面,如下所示:

public interface StoreItem {
//Operations
public String getMaker();
public Patron getItemBuyer();
public Date getManufactureDate();
public String getManufacturer();
public String getProductName();
public void setItemBuyer(Patron purchase);
    }

I have a method to create a new item and add it to the list. 我有一种创建新项目并将其添加到列表中的方法。 It seems to work ok, in that I'm not getting any problems on the unit test and no exceptions are caught. 看来工作正常,因为我在单元测试中没有任何问题,也没有捕获任何异常。 However I have that classic newbie problem where I want to print the arraylist to see the contents with my own eyes, and I understand I need to override the toString method for StoreItem. 但是我有一个经典的新手问题,我想打印arraylist以便用自己的眼睛看内容,而且我知道我需要为StoreItem覆盖toString方法。 But StoreItem is an interface and obviously it won't let me do anything like this within the Interface. 但是StoreItem是一个接口,显然,它不允许我在该接口内执行任何此类操作。 I also have an abstract class implementing this interface, and three subclasses of that abstract class. 我也有一个实现此接口的抽象类,以及该抽象类的三个子类。 I am creating an object of one of these subclasses as the StoreItem, so I tried to do a toString override within this subclass. 我正在创建这些子类之一的对象作为StoreItem,因此我尝试在此子类中进行toString重写。 I can't get this to work. 我无法使它正常工作。 I tried: 我试过了:

public String toString(){
    return super.productName();
}

Which it didn't like, because within the abstract super class productName is private, and needs to be for the sake of the project. 它不喜欢,因为在抽象超类中productName是私有的,并且需要用于项目。 I decided to go ahead and change productName to protected anyway just to see if it would still work. 我决定继续将productName更改为protected,只是为了看看它是否仍然有效。 I still just get the memory location (something like @d9sdf83j) when printing the list. 打印列表时,我仍然只是获得内存位置(类似@ d9sdf83j)。 So I set it back to private and tried to have the toString use the getter method from the superclass on the return, so: 因此,我将其设置回private,并尝试让toString在返回时使用超类的getter方法,因此:

public String toString(){
    return super.getProductName();
}

I still just get the memory location. 我仍然只是获得内存位置。 I don't know if this is enough code to portray what's going on and what I'm trying to do. 我不知道这是否足以描述正在发生的事情以及我想做的事情。 I'm hoping the error is rudimentary enough that someone wiser than me can just spot it and point it out, but I can add more code if necessary. 我希望该错误非常简单,以至于比我聪明的人可以发现并指出该错误,但是如有必要,我可以添加更多代码。 Thanks in advance. 提前致谢。

I still can't get it to work. 我仍然无法正常工作。 I'm going to add the actual classes and see if that helps. 我将添加实际的类,看看是否有帮助。

public abstract class AbstractStoreItem implements StoreItem{

//attributes
private String product;
private Date manufactureDate;
private String manufacturer;
private String distibutor;
private Patron buyer;

//operations
public AbstractStoreItem(String product, String manufacturer, String distibutor, Date manufactureDate) {
    this.product = product;
    this.manufactureDate = manufactureDate;
    this.manufacturer = manufacturer;
    this.distibutor = distibutor;
}

@Override
public String toString() {
    return this.product;

}

@Override
public String getManufacturer() {
    // TODO Auto-generated method stub
    return manufacturer;
}

@Override
public Patron getBuyer() {
    // TODO Auto-generated method stub
    return buyer;
}

@Override
public Date getManufactureDate() {
    // TODO Auto-generated method stub
    return manufacturerDate;
}

@Override
public String getDistributor() {
    // TODO Auto-generated method stub
    return distributor;
}

@Override
public String getProduct() {
    // TODO Auto-generated method stub
    return product;
}

@Override
public void setBuyer(Patron buyer) {
    this.buyer= buyer;

}

} }

Here is the subclass of the abstract class. 这是抽象类的子类。 This is the object I'm adding to the list: 这是我要添加到列表中的对象:

public class Widget extends AbstractStoreItem{

//attributes
private Type type;
private int unitQuantity;

//operations
public Widget(String product, String manufacturer, String distributor, Date manufactureDate, Type gearType, int unitQuantity){
    super(product, manufacturer, distributor, manufactureDate);

    this.type = gearType;
    this.unitQuantity = unitQuantity;
}

public Type getType(){
    return type;
}

public int getUnitQuantity(){
    return unitQuantity;
}

@Override
public void setBuyer(Patron Buyer) {
    // TODO Auto-generated method stub

}

@Override
public String toString() {
    return super.getManufacturer();
}

} }

Not sure what to do to the toString method. 不知道要对toString方法做什么。

You need to override toString in the class which implements StoreItem of which instances you are adding to List 您需要在实现StoreItem的类中重写toString ,该类将要添加到List实例

interface StoreItem {
    String name();
}

static class Pen implements StoreItem {

    @Override
    public String name() {
        return Pen.class.getName();
    }

    @Override
    public String toString() {
        return "Pen to String";
    }

}

public static void main(String[] args) {
    List<StoreItem> items= new ArrayList<StoreItem>();
    items.add(new Pen());
    System.out.println(items);
}

Will Print 将打印

 [Pen to String]

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

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