如何使用子类型?(How to use subtypes?

编程入门 行业动态 更新时间:2024-10-25 22:33:08
如何使用子类型?(How to use subtypes? - overriding and inheriting in Java)

我的Java代码存在问题。 我有四个(重要)类:

public class RDOutput extends OutputType public class RDAnalysis extends AnalysisProperties

现在我正在尝试在Analysis属性中创建一个方法:

public abstract void display(ArrayList<? extends OutputType> results);

主要问题列表,ArrayList中的对象将是OutputType的不同子类型。 在我的课程RDAnalysis中,我尝试进行特定的覆盖:

public void display(ArrayList<RDOutput> results) {

但是eclipse说:名称冲突:类型RDAnalysis的方法显示(ArrayList)与AnalysisProperties类型的显示(ArrayList?extends OutputType)具有相同的擦除但不覆盖它

我不熟悉Java技巧,我尝试在文档中搜索,但我找不到任何解决这个问题的方法。 我的问题是:我正在做的那种技巧(抽象的基本类型和最终函数中的扩展)可能在Java中(如果是的话,我该怎么做?)或者我必须做一些枚举来解决这个问题吗?

I've got problem in my code in Java. I have four(important) Classes:

public class RDOutput extends OutputType public class RDAnalysis extends AnalysisProperties

Now I'm trying to make a method in Analysis properties:

public abstract void display(ArrayList<? extends OutputType> results);

The main problem list, the objects in the ArrayList will be different subtypes of OutputType. In my class RDAnalysis I try to make specific overriding:

public void display(ArrayList<RDOutput> results) {

but eclipse says: Name clash: The method display(ArrayList) of type RDAnalysis has the same erasure as display(ArrayList? extends OutputType) of type AnalysisProperties but does not override it

I'm not familiar with Java tricks, I tried searching in documentation and I didn't find any solution to this problem. My question is: Is that trick that I'm doing (Basic type in abstract and Extended in final function) possible in Java (if yes, how can I do that?) or do I have to make some enum to solve this?

最满意答案

我建议你在你的类中引入泛型参数,并用它来参数化你的方法:

public abstract class A<T extends OutputType> { public abstract void display(ArrayList<T> results); } public class B extends A<RDOutput> { public void display(ArrayList<RDOutput> results) {} }

I suggest you to introduce generic parameter to your class and use it to parametrize your method:

public abstract class A<T extends OutputType> { public abstract void display(ArrayList<T> results); } public class B extends A<RDOutput> { public void display(ArrayList<RDOutput> results) {} }

更多推荐

本文发布于:2023-07-26 17:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   类型   subtypes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!