英文:
How to pass different object type for same method instead of writing two methods with same functionality and return type in java
问题
以下是我想要使其动态的方法,它应该接受类类型作为参数,然后我可以在我的断言中使用它。我尝试过使用 instanceof,但没有成功。
public static <T> String setStatus(List<T> s, Predicate<T> ifAllCompleted, Predicate<T> ifAllNotStarted) {
String finalStatus = "";
if (s.stream().allMatch(ifAllCompleted)) {
finalStatus = "Complete";
} else if (s.stream().allMatch(ifAllNotStarted)) {
finalStatus = "Not Started";
} else {
finalStatus = "In-progress";
}
return finalStatus;
}
使用示例:
Predicate<SmeObject> smeAllCompleted = x -> x.getStatus().equals("Completed");
Predicate<SmeObject> smeAllNotStarted = x -> x.getStatus().equals("Not Started");
String smeStatus = setStatus(smeList, smeAllCompleted, smeAllNotStarted);
Predicate<ControllerObject> controllerAllCompleted = x -> x.getStatus().equals("Completed");
Predicate<ControllerObject> controllerAllNotStarted = x -> x.getStatus().equals("Not Started");
String controllerStatus = setStatus(controllerList, controllerAllCompleted, controllerAllNotStarted);
英文:
here is my methods i want to make it dynamic which should accept class type as parameter and then i can use that in my predicate i have tried using instanceof but didnt work
public static String setStatus(List<SmeObject> s) {
String finalStatus = "";
Predicate<SmeObject> ifAllCompleted = x.getStatus().equals("Completed");
Predicate<SmeObject> ifAllNotStarted = x.getStatus().equals("Not Started");
if (s.stream().allMatch(ifAllCompleted)) {
finalStatus = "Complete";
} else if (s.stream().allMatch(ifAllNotStarted)) {
finalStatus = "Not Started";
} else {
finalStatus = "In-progress";
}
return finalStatus;
}
public static String setStatus(List<ControllerObject> s) {
String finalStatus = "";
Predicate<ControllerObject> ifAllCompleted = x.getStatus().equals("Completed");
Predicate<ControllerObject> ifAllNotStarted = x.getStatus().equals("Not Started");
if (s.stream().allMatch(ifAllCompleted)) {
finalStatus = "Complete";
} else if (s.stream().allMatch(ifAllNotStarted)) {
finalStatus = "Not Started";
} else {
finalStatus = "In-progress";
}
return finalStatus;
}
答案1
得分: 0
如果 ControllerObject
和 SmeObject
共享一个公共父类,该父类公开了 getStatus()
方法,那么您可能希望使您的方法接受一个带有 List<? extends CommonObjectSuperType>
的列表:
public static String setStatus(List<? extends CommonObjectSuperType> s) {
String finalStatus = "";
Predicate<CommonObjectSuperType> ifAllCompleted = x.getStatus().equals("Completed");
Predicate<CommonObjectSuperType> ifAllNotStarted = x.getStatus().equals("Not Started");
...
}
然而,如果 ControllerObject
和 SmeObject
没有公共父类来公开 getStatus()
方法,并且您不能更改它们,那么您只能通过使方法泛型化来让调用者传递一个谓词:
public static <T> String setStatus(List<T> s, Predicate<T> allCompletedTest,
Predicate<T> allNotStartedTest) {
// 在方法中使用传入的谓词,而不是在方法中创建它们
...
}
然后调用者将在实际类型的上下文中创建谓词。
英文:
If ControllerObject
and SmeObject
share a common parent that exposes the getStatus()
method, then you may want to make your method take a list with a List<? extends CommonObjectSuperType>
:
public static String setStatus(List<? extends CommonObjectSuperType> s) {
String finalStatus = "";
Predicate<CommonObjectSuperType> ifAllCompleted = x.getStatus().equals("Completed");
Predicate<CommonObjectSuperType> ifAllNotStarted = x.getStatus().equals("Not Started");
...
}
If, however, ControllerObject
and SmeObject
do not have a common parent that exposes getStatus()
and you can't change them, then you're left with the option of making the caller send in a predicate by making the method generic:
public static <T> String setStatus(List<T> s, Predicate<T> allCompletedTest,
Predicate<T> allNotStartedTest) {
//use incoming predicates instead of creating them in the method
...
}
Then the caller will create the predicates in the context of the actual type.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论