英文:
Need an abstract function that can return different types of arrays
问题
以下是翻译好的内容:
我正在使用一个具有已被重载的方法JPanel makeBarGraph(Type[] dataX, Type[] dataY);
的库。 "Type" 可以是 String、Integer、LocalDateTime... 以及许多其他类型。
我目前有一个数据类 Post,它具有几个不同类型的字段。我已经创建了一个包含数据类中每个字段的枚举,名为 "Content",并且我创建了一个抽象函数叫做getContent()
,枚举中的每个项目都实现了它,如下所示:
AUTHOR {
public String[] getContent(SocialMediaFramework f, Post[] p, String data) {
}
},
LIKES {
public Integer[] getContent(SocialMediaFramework f, Post[] p, String data) {
}
};
public abstract String[] getContent(SocialMediaFramework f, Post[] p);
public abstract Integer[] getContent(SocialMediaFramework f, Post[] p);
该函数应该返回一个特定类型的数组。这样,我可以在主类中编写以下函数:
public void displayBarGraph(Content type1, Content type2) {
frame.add(makeBarGraph(type1.getContent(), type2.getContent());
}
问题是,你不能通过区分返回类型来在 Java 中重载方法,所以我的代码无法编译。有没有另一种方法可以做到这一点,以便我不必诉诸于一长串的 if else 或 case 语句?
英文:
I am working with a library with a method JPanel makeBarGraph(Type[] dataX, Type[] dataY);
that has been overloaded. "Type" can be String, Integer, LocalDateTime... and many other types.
I currently have a data class, Post, that has several fields of several different types. I have made an enum that contains every field in this data class called "Content" and I made an abstract function called getContent()
that each item in the enum implements like so:
AUTHOR {
public String[] getContent(SocialMediaFramework f, Post[] p, String data) {
}
},
LIKES {
public Integer[] getContent(SocialMediaFramework f, Post[] p, String data) {
}
};
public abstract String[] getContent(SocialMediaFramework f, Post[] p);
public abstract Integer[] getContent(SocialMediaFramework f, Post[] p);
The function is supposed to return an array of a specific type. This way, I can write the following function in my main class:
public void displayBarGraph(Content type1, Content type2) {
frame.add(makeBarGraph(type1.getContent(), type2.getContent());
}
The problem is, you can't overload methods in Java by differentiating their return type, so my code won't compile. Is there another way of doing this so that I don't have to resort to a long list of if else or case statements?
答案1
得分: 0
请使用泛型类型
Stackoverflow参考 如何使方法的返回类型为泛型?
答案2
得分: 0
# 调用哪个重载方法是在编译时确定的 #
### 请参考[Java中的静态绑定与动态绑定][1] ###
由于重载方法使用**静态绑定**,而静态绑定发生在**编译时**,即使您对泛型方法`getContent`为每个枚举返回不同的类型,编译器也无法在编译时确定调用哪个重载的`makeBarGraph`。
建议
---
假设您期望的结果是`JPanel`,您可以在枚举类中添加一个抽象方法`JPanel getBarGraph`。然后枚举中的每个实例都可以实现此方法,并在库中调用所需的重载`makeBarGraph`方法。
[1]: https://stackoverflow.com/questions/19017258/static-vs-dynamic-binding-in-java
英文:
Which Overrloaded method is called is determined in compile time
By referring to Static Vs. Dynamic Binding in Java
As overloaded methods are bonded using static binding, and static binding occur in compile time, even you have generic method getContent
return different type for each enum, compiler can't tell which overloaded makeBarGraph
you are calling in compile time.
Suggestion
Suppose the outcome you are looking for is the JPanel
, you can add an abstract method JPanel getBarGraph
in the enum class. Then every instance in the enum can implement this method and call desired overloaded makeBarGraph
method in the library.
专注分享java语言的经验与见解,让所有开发者获益!
评论