演员表列出<我的界面>

发布于 2024-12-07 07:44:27 字数 430 浏览 1 评论 0原文

我正在用java编写一段代码,并且想封装我的对象并且只返回接口。

简而言之,这是我的问题,我有一个包含列表的类,并且我有一个返回列表的 getter。如何返回接口列表?

如果没有收到此错误警告:

列表是原始类型。对泛型类型 List 的引用应该参数化

为了让您清楚地了解这段代码是我的问题的一个示例:

public class Employee{

  private List<MyObject> myObjects;

  public List<MyInterface> getMyObjects(){
    return myObjects;
  }

}

其中 MyObject 实现 MyInterface 并返回 myObjects 给出了问题。

提前致谢。

I'm writing a piece of code in java and would like to encapsulate my object and only return interfaces.

Now in short this is my problem I have a class containing a list, and I have a getter which returns a List. How can I return a list of the interface ?

Without getting this error warning :

List is a raw type. References to generic type List should be parameterized

To give you a clear picture this piece of code is an example of my problem :

public class Employee{

  private List<MyObject> myObjects;

  public List<MyInterface> getMyObjects(){
    return myObjects;
  }

}

where MyObject implements MyInterface and return myObjects gives the problem.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

携君以终年 2024-12-14 07:44:27

如果 MyObject 实现 MyInterface 您可以将返回类型更改为 List

class Employee{
  private List<MyObject> myObjects;

  public List<? extends MyInterface> getMyObjects(){
     return myObjects;
  }
}

请注意,在这种情况下,编译器不允许您在返回的列表上调用 add(...),除非您在此处进行强制转换。但是,由于您仅返回接口,我想您不打算向返回的列表添加任何内容,所以应该没问题。

If MyObject implements MyInterface you could change the return type to List<? extends MyInterface>.

class Employee{
  private List<MyObject> myObjects;

  public List<? extends MyInterface> getMyObjects(){
     return myObjects;
  }
}

Note that in this case the compiler won't allow you to call add(...) on the returned list, except you'd cast here. However, since you're returning interfaces only, I guess you're not planning to add anything to the returned list, so you should be fine.

匿名的好友 2024-12-14 07:44:27

只需将接口列表声明为实例变量即可。

private List<MyInterface> myObjects;

Simply declare a list of your interface as your instance variable.

private List<MyInterface> myObjects;
反话 2024-12-14 07:44:27

我只是按照您需要的方式定义实例属性:

public class Employee{

  private List<MyInterface> myObjects;

  public List<MyInterface> getMyObjects(){
    return myObjects;
  }

}

I'd simply define the instance attribute the way you need it:

public class Employee{

  private List<MyInterface> myObjects;

  public List<MyInterface> getMyObjects(){
    return myObjects;
  }

}
望她远 2024-12-14 07:44:27

尽管 MyObject 实现了 MyInterface,但 List 扩展 List为真。 ; - 请参阅这篇优秀文档了解更多信息。为了告知编译器您的意图,请指定方法签名,如下所示:

public List<? extends MyInterface> getMyObjects{
    ...

Even though MyObject implements MyInterface, it is not true that List<MyObject> extends List<MyInterface> - see this excellent document for more info. In order to inform the compiler of your intentions, specify the method signature like so:

public List<? extends MyInterface> getMyObjects{
    ...
躲猫猫 2024-12-14 07:44:27

您无法将 List 转换为 List。因此,尝试将 myObjects 定义为 List 或使用以下内容:

public class Employee{
  private List<MyObject> myObjects;

  public List<MyInterface> getMyObjects(){
    List<MyInterface> result = new ArrayList<MyInterface>(myObjects.size());
    result.addAll(result);
    return result;
  }
}

You cannot cast List<MyObject> to List<MyInterface>. So try to define myObjects as List<MyInterface> or use the following:

public class Employee{
  private List<MyObject> myObjects;

  public List<MyInterface> getMyObjects(){
    List<MyInterface> result = new ArrayList<MyInterface>(myObjects.size());
    result.addAll(result);
    return result;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文