如何通过包含完全相同类型参数的参数列表的方法来重载方法,但参数化为其他类型 [英] How to overload a method by method with parameters list that contains parameters of the exact same type but parametrized with other types

查看:84
本文介绍了如何通过包含完全相同类型参数的参数列表的方法来重载方法,但参数化为其他类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法:

  public List< Integer> convertBy(功能<字符串,列表<字符串>> flines,功能与LT;列表与LT;字符串>中的字符串>加入,功能与LT;字符串,列表<整数>> collectInts){
返回collectInts.apply(join.apply (flines.apply((String)value)));
} //第一方法

公共整数convertBy(功能<列表与LT;字符串>中的字符串>加入,功能与LT;字符串,列表<整数>> collectInts,功能与LT;列表与LT;整数> ,Integer> sum){
return sum.apply(collectInts.apply(join.apply((List< String>)value)));
} //第二种方法

尽管它们的参数用不同的类型参数化,第一种方法。除了 Function< T,R> 以外,我可能会使用不同的接口,但不知道哪一个接口足够了,因为我浏览了它们的列表并找不到一个< a href =https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html =nofollow> https://docs.oracle.com/ javase / 8 / docs / api / java / util / function / package-summary.html 。



这些函数的参数是:



flines - 从给定路径读取文件( String )并返回列表( List< String>



join - 连接给定 List< String> 的元素并返回一个 String



collectInts - 解析给定的字符串并返回在<$ c中找到的整数列表
$ b $ sum - 从 List< Integers> 并返回总和



问题:




  1. 我可以通过第二个方法重载第一个方法吗?


  2. 除了函数,我还可以使用其他现有的函数接口吗?我认为没有,因为参数和结果的类型总是不一样的。

  3. >如果你想创建一个应用多个函数并且对中间值不感兴趣的方法,你可以使它成为一个通用的方法。你的问题中的代码很奇怪,因为它假定 value 可以是一个字符串和一个清单< String>



    但与你的其他问题,这里有一个不同的图片。虽然varargs方法不能以这种方式工作,但您可以轻松地为实际用例提供重载方法:

      public class InputConverter< T> {

    私人T值;

    public InputConverter(T value){
    this.value = value;
    }
    public< R> R convertBy(函数<?super T,?extends R> f){
    return f.apply(value);
    }
    public< T1,R> R convertBy(
    函数<?super T,?extend T1> f1,Function< super T1,?extends R> f2){
    return f2.apply(f1.apply(value));
    }
    public< T1,T2,R> R convertBy(
    函数<?super T,?extend T1> f1,Function<?super T1,?extends T2> f2,
    函数<?super T2,?extends R> f3){
    return f3.apply(f2.apply(f1.apply(value)));
    }
    public< T1,T2,T3,R> R convertBy(
    函数<?super T,?extends T1> f1,Function<?super T1,?extends T2> f2,
    函数<?super T2,?extends T3> f3,Function<? super T3,?extends R> f4){
    return f4.apply(f3.apply(f2.apply(f1.apply(value))));






    $ b假设你将你的接口类型和创建的函数修改为在这个答案中描述,您可以像使用它

      InputConverter<字符串> fileConv = new InputConverter<>(LamComFile.txt); 
    列表< String> lines = fileConv.convertBy(flines);
    String text = fileConv.convertBy(flines,join);
    列表<整数> ints = fileConv.convertBy(flines,join,collectInts);
    整数sumints = fileConv.convertBy(flines,join,collectInts,sum);


    I have a methods:

    public List<Integer> convertBy(Function<String, List<String>> flines, Function<List<String>, String> join, Function<String, List<Integer>> collectInts) {
        return collectInts.apply(join.apply(flines.apply((String) value)));
    }//first method
    
    public Integer convertBy(Function<List<String>, String> join, Function<String, List<Integer>> collectInts, Function<List<Integer>, Integer> sum) {
        return sum.apply(collectInts.apply(join.apply((List<String>) value)));
    }//second method
    

    Despite their parameteres are parametrized with different types I cannot overload the first method. I might use different interface, other than Function<T,R> but don't know which one would suffice as I went through list of them and couldn't find one https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html.

    Parameters in those functions are:

    flines - reads the file from given path (String) and returns list of lines in that file (List<String>)

    join - concatenates element of given List<String> and returns a String

    collectInts - parses the given String and returns List of integers found in that String.

    sum - adds elements from List<Integers> and returns the sum

    Questions:

    1. Can I overload the first mehod by the second one?

    2. What other existing functional interface I might use besides function? I think none, as the types of argument and result always differ.

    解决方案

    If you want to create a method which applies multiple functions and is not interested in the intermediate values, you can make it a generic method. The code in your question is strange as it assumes that value can be a String and a List<String> at the same time.

    But comparing with your other question, there’s a different picture. While the varargs method there can’t work that way, you can easily provide overloaded methods for the actual use cases:

    public class InputConverter<T> {
    
        private T value;
    
        public InputConverter(T value) {
            this.value = value;
        }
        public <R> R convertBy(Function<? super T, ? extends R> f) {
            return f.apply(value);
        }
        public <T1,R> R convertBy(
            Function<? super T, ? extends T1> f1, Function<? super T1, ? extends R> f2) {
            return f2.apply(f1.apply(value));
        }
        public <T1,T2,R> R convertBy(
            Function<? super T, ? extends T1> f1, Function<? super T1, ? extends T2> f2,
            Function<? super T2, ? extends R> f3) {
            return f3.apply(f2.apply(f1.apply(value)));
        }
        public <T1,T2,T3,R> R convertBy(
            Function<? super T, ? extends T1> f1, Function<? super T1, ? extends T2> f2,
            Function<? super T2, ? extends T3> f3, Function<? super T3, ? extends R> f4) {
            return f4.apply(f3.apply(f2.apply(f1.apply(value))));
        }
    }
    

    Assuming that you fixed your interface types and created functions as described in this answer, you can use it like

    InputConverter<String> fileConv=new InputConverter<>("LamComFile.txt");
    List<String> lines = fileConv.convertBy(flines);
    String text = fileConv.convertBy(flines, join);
    List<Integer> ints = fileConv.convertBy(flines, join, collectInts);
    Integer sumints = fileConv.convertBy(flines, join, collectInts, sum);
    

    这篇关于如何通过包含完全相同类型参数的参数列表的方法来重载方法,但参数化为其他类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆