1

我已经四处寻找这个特定问题的帮助,也许我的措辞是错误的,如果是这样,一个链接将是完美的!

这是问题所在:

我有一个界面如下:

Product get(String prodCode);

我还有一个实现接口的抽象类,它具有以下内容:

public Product get(String prodCode) { //Do nothing }

但是,为了满足编译器的要求,我必须返回一些东西(null 或 Product)。我的理解是它不应该返回这些东西,因为它不再“什么都不做”。我不确定如何在什么都不做的同时满足编译器。

4

2 回答 2

0

Based on the information from your comment, I would say that you could do either one of the following:

  1. Return null.
  2. Return a default Product object which has no functionality.

Returning null would be the simplest, but you would want to make sure that calling code protects itself from NullPointerException's that could result from attempting to interact with the returned result.

If you were to do the second, a static final object would probably be ideal to return.

于 2012-10-31T19:40:38.367 回答
0

我不知道您的 DoNothingProduct 实现会是什么样子,但是您可以为 Product 设置一个无参数的默认构造函数,该构造函数将返回一个设置了默认字段的不可变对象。

于 2012-10-31T19:31:33.657 回答