Java接口作为参数帮助 [英] Java Interface as argument help

查看:67
本文介绍了Java接口作为参数帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件如下:

ICharacterReader.java ::

I have 2 files which are as follows:
ICharacterReader.java::

import java.io.EOFException;

    public interface ICharacterReader {
      char GetNextChar() throws EOFException;
      void Dispose();
    }



和另一个文件:

SimpleCharacterReader.java ::


and another file:
SimpleCharacterReader.java::

import java.io.EOFException;
import java.util.Random;

public class SimpleCharacterReader implements ICharacterReader {
  private int m_Pos = 0;

  public static final char lf = '\n';

  private String m_Content =
    "It was the best of times, it was the worst of times," +lf +
    "it was the age of wisdom, it was the age of foolishness,";

  Random m_Rnd = new Random();

  public char GetNextChar() throws EOFException {

    if (m_Pos >= m_Content.length()) {
        throw new EOFException();
    }

    return m_Content.charAt(m_Pos++);

  }

  public void Dispose() {
    // do nothing
  }
}



我的任务如下:

1.)编写一个以ICharacterReader接口为参数的类,并返回按字排序的字频率列表按字母顺序计算。



并编写一个控制台应用程序的主方法,使用SimpleCharacterReader练习此类,并将输出打印到控制台。



例如,如果流返回这是最好的时间,那是最糟糕的时间,那么输出将是:



it - 2;

of - 2;

the - 2;

乘以-2;

是 - 2;

最好 - 1;

最差 - 1;

2.)测试第1部分中的答案,通过编写单元测试用例。



你能帮我传递接口作为参数吗?写单元测试用例是什么意思?


My task is as follows:
1.) Write a class that takes an ICharacterReader interface as an argument and returns a list of word frequencies ordered by word count and then alphabetically.

And also write a main method of a console application that exercises this class using a SimpleCharacterReader, and prints the output to the console.

For example, if the stream returns "It was the best of times, it was the worst of times" then the output will be:

it - 2;
of - 2;
the - 2;
times -2;
was - 2;
best - 1;
worst - 1;
2.) Test the answers in part 1, by writing unit test cases.

Could you help me with passing interface as an argument and what is meant by "writing unit test case"?

推荐答案

我写了一个概念证明作为指导。 br />


接口:

I wrote a proof of concept as a guide.

Interface:
public interface IVehicle {
    int GetNumWheels();
}





实施课程:



Implementing class:

public class Car implements IVehicle{

    @Override
    public int GetNumWheels() {
        return 4;
    }

}





主要:



Main:

public class ABC {
    public ABC(){}

    public int NumWheels(IVehicle v)
    {
        return v.GetNumWheels();
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ABC a=new ABC();
        Car c=new Car();
        String ans = String.format("%s has %d wheels", c.getClass().getName(), a.NumWheels(c));
        System.out.println(ans);
    }

}


这篇关于Java接口作为参数帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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