如何在声明它的类之外调用java.util.function.Function?

时间:2016-10-20 21:15:36

标签: java functional-programming

我是java函数式编程和函数库的新手,我想将它用于 apply 函数。我希望能够将对象转换为另一种类型。

我们说我有这两个对象:

public class UserOne {
   private String firstName;
   private String lastName;
   ... constructor/getters and setters
}

public class UserTwo {
   private String name;
   ... constructor/get/set
}

然后我用我的函数创建一个新类:

public class TransformUser1ToUser2 {
   Function<UserOne, UserTwo> user1ToUser2 = new Function<UserOne, UserTwo>() {
      public UserTwo apply(UserOne userone) {
         UserTwo usertwo = new usertwo;
         usertwo.setname(t.getFirstName() + " " + t.getLastName());
         return usertwo;
      }
   }
}

现在我应该如何在另一个班级中调用 apply 方法,让我们说:

public class MainClass {
   public static void main() {
      UserOne userone = new UserOne("firstName", "lastName");
      UserTwo usertwo = CALL Function to transform userone to usertwo
   ...
   }
}

0 个答案:

没有答案