简体   繁体   English

我如何在同一个 class 的不同方法中使用私有 static 方法(对象的扩展)的返回值? (爪哇)

[英]How can i use the return value of a private static method (extension of object) in a different method in that same class? (java)

I have a java project with 3 classes.我有一个 java 项目,有 3 个班级。 1 class is used as a static private method in another class. I need to add the results of that method to be added to a list in another method within that class. 1 class 在另一个 class 中用作 static 私有方法。我需要将该方法的结果添加到该 class 中另一个方法的列表中。

Is there anyone who can send me in a good direction?有没有人可以送我一个好的方向?

I understand that you want the implementation of method validWords() .我知道您想要方法validWords()的实现。 The following works for me.以下对我有用。

public static List<WordScore> validWords(final List<String> wordList, final int[] tileCounts) {
    return wordList.stream()
                   .map(word -> wordScore(word, tileCounts))
                   .collect(Collectors.toList());
}

The above code uses the stream API that was added in Java 8. You can also refer to the lesson entitled Aggregate Operations in Oracle's Java tutorials.上述代码使用了Java 8中新增的stream API,也可以参考Oracle的Java教程中的聚合操作一课。

Method stream() that is inherited by interface java.util.List returns a "stream" of all the elements in wordList .接口java.util.List继承的方法stream()返回wordList中所有元素的“流”。 Each element is a string.每个元素都是一个字符串。

Method map takes a single parameter which is a functional interface .方法map采用一个函数接口参数。 A functional interface can be implemented by a lambda expression .函数式接口可以通过lambda 表达式来实现。 The method that needs to be implemented takes a single string parameter and returns a WordScore object.需要实现的方法采用单个字符串参数并返回WordScore object。

Method collect() also takes a single parameter that is a functional interface.方法collect()还采用一个函数接口参数。 Utility class Collectors contains an implementation that returns a List that contains all the elements in the stream. In this case the stream contains WordScore elements.实用程序 class Collectors包含一个实现,该实现返回一个包含 stream 中所有元素的List 。在这种情况下,stream 包含WordScore元素。 Hence collect() in the above code returns the list that you want the method validWords() to return.因此,上面代码中的collect()返回您希望方法validWords()返回的列表。

If this is not what you are looking for, let me know and I will delete it.如果这不是您要查找的内容,请告诉我,我会删除它。

Member functions of the same class can call and pass data to each other.同一个class的成员函数可以互相调用和传递数据。 You just need to call wordScore() from validWords() for each string in the list.您只需为列表中的每个字符串从validWords()调用wordScore()

public static List<WordScore> validWords (final List<String> wordList, final int[] tileCounts) {
    List<WordScore> results = new ArrayList<WordScore>();
    for(String word : wordList) {
        results.add(wordScore(word, tileCounts));
    }
    return results;
}

This is the simple way.这是简单的方法。 @Abra's code also works, it uses the Stream API introduced in Java 8. Java 8 has also introduced lambda expressions which will help simplify your Comparator : @Abra 的代码也有效,它使用 Java 8 中介绍的 Stream API。Java 8 还介绍了 lambda 表达式,这将有助于简化您的Comparator

Collections.sort(list, new Comparator<WordScore>()
        {
            @Override
            public int compare(final WordScore wsA, final WordScore wsB)
            {
                return (wsA.score() == wsB.score())
                        ? 0
                        : (wsA.score() > wsB.score()) ? -1 : 1;
            }
        });

This can be rewritten as:这可以重写为:


Collections.sort(list, (wsA) -> (wsA.score() == wsB.score())
                        ? 0
                        : (wsA.score() > wsB.score()) ? -1 : 1);
         
    

This works because Comparator is a functional interface, an interface that contains only one method compare() .这是有效的,因为Comparator是一个功能接口,一个只包含一个方法compare()的接口。 Instead of creating an anonymous class and overriding compare() , what you do is simply specify the arguments in parantheses (type isn't required, the compiler is smart enough to infer them) followed by the -> operator.无需创建匿名 class 并覆盖compare() ,您只需在括号中指定 arguments(不需要类型,编译器足够聪明,可以推断出它们),然后是->运算符。 After this, you specify the return value, the return keyword can also be omitted.在此之后,您指定返回值, return关键字也可以省略。

You also don't have to do these multiple condition checks, simply return the difference of the scores:您也不必进行这些多重条件检查,只需返回分数的差异:

Collections.sort(list, (wsA, wsB) ->  wsA.score() - wsB.score() );

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

相关问题 如何使用mockito存根同一类的私有方法的返回值 - How to stub return value for the private method of same class using mockito 我可以将getter方法的返回值传递给不同类中另一个对象的setter方法吗? - Can I Pass a return value of a getter method to a setter method of another object in a different class? 如何使公共静态非同步getInstance()方法将私有静态引用变量的多个实例返回给对象? - How can I make a public static unsynchronized getInstance() method return multiple instances of a private static reference variable to an object? 静态方法如何返回不同的实现类? - How can a static method return a different implementation class? 为什么我在Java中的同一个类中没有抽象和静态方法 - Why can I not have an abstract and static method in the same class in Java JAVA如何在同一类的非静态方法中使用新返回的对象的各个方面 - JAVA How to use aspects of newly returned object in a non-static method of same class 我可以使用其他类的静态方法吗? - Can I use static method from a different class? Java 8:如何将静态方法用作另一个方法的参数? - Java 8: How can I use a static method as a parameter for another method? 如何返回多个变量和/或使用私有int方法? - How can I return multiple variables and/or use a private int method? 如何在其他类中使用方法? - How can I use a method in a different class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM