简体   繁体   English

如何为同一方法传递不同的 object 类型,而不是在 java 中编写具有相同功能和返回类型的两个方法

[英]How to pass different object type for same method instead of writing two methods with same functionality and return type in java

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这是我想让它动态化的方法,它应该接受 class 类型作为参数,然后我可以在我的谓词中使用它我已经尝试使用 instanceof 但没有用

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;
}

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>如果ControllerObjectSmeObject共享一个公开getStatus()方法的公共父级,那么您可能希望让您的方法获取一个带有List<? extends CommonObjectSuperType> List<? extends CommonObjectSuperType> : 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:但是,如果ControllerObjectSmeObject没有公开getStatus()的公共父级并且您无法更改它们,那么您可以选择通过使方法通用来让调用者发送谓词:

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.然后调用者将在实际类型的上下文中创建谓词。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JAva:如何将自定义对象类型作为参数传递给方法,并返回相同的自定义对象类型值 - JAva:How to pass a custom object type as a parameter to a method and return the same custom object type value back 两个接口中具有相同签名但返回类型不同的方法 - Methods with the same signature but different return type in two interfaces 相同方法的不同返回类型 - Different return type for the same method java:传递对象并返回相同对象的类型 - java: pass an object and return same object's type java泛型方法的返回类型应该与传递的传递参数相同 - java generics return type of method should be same pass parameter passed 如何将两个返回类型不同但主体相同的方法与 throw 语句结合起来? - How to combine two methods with different return type but same body with throw statement? 使用相同的方法签名但不同的返回类型实现两个接口 - Implement two interfaces with the same method signature but different return type 两个接口具有相同的方法名称和不同的返回类型 - Two interface have same method name with different return type 使用具有相同方法但返回类型不同的两个接口的Java多重继承 - Java multiple inheritance using two interface having same method but different return type Java 8 - 两个接口包含具有相同方法签名但返回类型不同的默认方法,如何覆盖? - Java 8 -Two interfaces contain default methods with the same method signature but different return types, how to override?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM