【问题标题】:Inner interface class内部接口类
【发布时间】:2015-07-07 12:40:28
【问题描述】:

我对内部类进行了测试并返回平均值,但我无法让它正常工作。

我有一个带有 2 个方法的内部接口的类,我需要从另一个类中对其进行测试,但似乎没有任何效果。

我尝试了很多方法,但仍然不知道哪里出了问题?

有人可以看看并尝试告诉我我做错了什么或告诉我需要做什么来测试 BetterProgrammerTask 类吗?

我将添加具有内部接口的类和尝试使用它的测试类。

测试程序是:

public class BetterProgrammerTask {

    // Please do not change this interface
    public static interface Node {
        int getValue();

        List<Node> getChildren();
    }

    // Please do not change this interface

    public static double getAverage(Node root) {
        /*
          Please implement this method to
          return the average of all node values (Node.getValue()) in the tree.
         */
        int suma = root.getValue() + suma(root.getChildren());
        int count = 1 + count(root.getChildren());
        return suma / count;
    }

    private static int suma(List<Node> nodes) {
        if (nodes == null || nodes.isEmpty()) {
            return 0;
        }
        int suma = 0;
        for (Node n : nodes) {
            suma += n.getValue() + suma(n.getChildren());
        }
        return suma;
    }

    private static int count(List<Node> nodes) {
        if (nodes == null || nodes.isEmpty()) {
            return 0;
        }
        int suma = 0;
        for (Node n : nodes) {
            suma += 1 + count(n.getChildren());
        }
        return suma;
    }
}

带有测试方法的类是:

import main.java.org.example.BetterProgrammerTask.Node;

public class TestBetterProgrammerTask implements BetterProgrammerTask.Node {

    public static void main(String[] args) {

        int sum = 0;
        double sumDouble = 0;
        System.out.println("EXAMPLE III");
        // get NodeImpl

        //
        // getAverage does not accept my node definition ?????
        //
        sumDouble = getAverage(Node);


        DecimalFormat four = new DecimalFormat("#0.00");
        System.out.println("For this process the node average calculated is  " + four.format(sumDouble));
    }

    //
    // and average and getChildren are not filled yet and still did not 
    // due to errors in the getAverage
    //
    @Override
    public int getValue() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<BetterProgrammerTask.Node> getChildren() {
        // TODO Auto-generated method stub
        return null;
    }
}

【问题讨论】:

  • 很抱歉,但忘了向看到这个问题的任何人打招呼。 :-)
  • 这实际上是件好事,我们正在努力建立问题->解决方案站点,所以“你好”、“提前感谢”是不必要的。更多信息meta.stackexchange.com/questions/2950/…
  • sumDouble=getAverage(new TestBetterProgrammerTask()); 因为Node 是一个接口,它需要一个Node 的实例。
  • 嗨添加一个新的 sumDouble=getAverage(new TestBetterProgrammerTask());由于仍然未定义节点类型而无法工作

标签: java class inner-classes


【解决方案1】:

import static 来导入像getAverage 这样的方法。并创建一个Node的实例

package main.java.org.example;

import java.text.DecimalFormat;
import java.util.List;

import static main.java.org.example.BetterProgrammerTask.getAverage;

public class TestBetterProgrammerTask implements BetterProgrammerTask.Node {

    public static void main(String[] args) {

        int sum = 0;
        double sumDouble = 0;
        System.out.println("EXAMPLE III");
        // get NodeImpl

        BetterProgrammerTask.Node node = new TestBetterProgrammerTask();
        sumDouble = getAverage(node);

//rest unchanged

注意:我也不建议使用 main.java 作为包的一部分。

NB2:如果您使用的是 Java 8,则可以使用 Default Methods。并将 getAverage 作为方法,在节点接口上实现,不必静态导入方法并将其作为 @ 的成员调用987654328@.

【讨论】:

  • 我使用 main.java 因为使用 maven 编译。我实现了结果,一切正常。
  • 好的,然后接受答案
  • 顺便说一句,你的 maven 设置一定是错误的,这是不正常的。也许你应该问这个问题。
猜你喜欢
  • 2011-12-03
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多