【问题标题】:How do I use @Override toString with an ArrayList of interface objects?如何将 @Override toString 与接口对象的 ArrayList 一起使用?
【发布时间】:2012-12-14 17:49:24
【问题描述】:

我对接口的理解是它们不能包含方法定义,只能包含签名和常量声明。我有一个包含接口对象集合的数组列表。在这种情况下,它看起来像:

List items = new ArrayList();

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

我有一种方法可以创建一个新项目并将其添加到列表中。它似乎工作正常,因为我在单元测试中没有遇到任何问题,也没有发现异常。但是我有一个经典的新手问题,我想打印数组列表以亲眼看到内容,并且我知道我需要覆盖 StoreItem 的 toString 方法。但是 StoreItem 是一个接口,显然它不会让我在接口内做这样的事情。我还有一个实现这个接口的抽象类,以及那个抽象类的三个子类。我正在创建这些子类之一的对象作为 StoreItem,因此我尝试在此子类中执行 toString 覆盖。我不能让它工作。我试过了:

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

它不喜欢,因为在抽象超类中 productName 是私有的,并且为了项目需要。我决定继续将 productName 更改为 protected ,只是为了看看它是否仍然有效。打印列表时,我仍然只是获取内存位置(类似于@d9sdf83j)。所以我将它设置回私有并尝试让 toString 在返回时使用超类中的 getter 方法,所以:

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

我仍然只是得到内存位置。我不知道这是否足以描述正在发生的事情以及我正在尝试做的事情。我希望这个错误足够基本,以至于比我更聪明的人可以发现它并指出它,但如果需要,我可以添加更多代码。提前致谢。

我仍然无法让它工作。我将添加实际的类,看看是否有帮助。

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;

}

}

这里是抽象类的子类。这是我要添加到列表中的对象:

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

}

不确定如何处理 toString 方法。

【问题讨论】:

  • 听起来你没有在你实际添加到ArrayList的元素的类中实现toString()。

标签: java interface arraylist


【解决方案1】:

您需要在实现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);
}

将打印

 [Pen to String]

【讨论】:

  • 我正在尝试这样做,但它仍然无法正常工作。所以我有实现 StoreItem 的 StoreItem、AbstractStoreItem,以及扩展 AbstractStoreItem 的 Widget、Wrench 和 Wheel。我将小部件创建为 StoreItems 并将它们放入列表中。我尝试添加 toString 方法 AbstractStoreItem、Widget、Wrench 和 Wheel,但我仍然只是胡言乱语。 AbstractStoreItem 已经有一个 toString 方法,看起来像这样: public String toString() { return super.toString(): } 这样的东西不正确吗?
  • return super.toString(); 不会为您服务,因为它将与您需要在某处提供实现的默认 toString 实现相同
  • 我可以使用 AbstractStoreItem 中的属性返回吗?我想我很难确定我实际上可以返回什么
  • 是的。使用可能返回您想要观察的状态的属性或方法。在AbstractStoreItem 中为toString 提供一些人类可读的表示,然后您可以根据需要在子类中进行更改或保持不变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
相关资源
最近更新 更多