java - Java - 在类定义中找不到错误
问题描述
我对以下代码有疑问。这是一项考试任务,我无法弄清楚给定代码中的错误是什么。我们应该在 A 类中找到错误,该错误会阻止我们使用 JUnit 测试代码。
import java.util.List;
import java.util.ArrayList;
public class A {
private List<String> s;
public A() {
s = new ArrayList<String>();
s.add("Bob");
s.add("Alice");
s.add("Eve");
}
public String s(B b){
int t = b.t();
String r = "Hello ";
for (String z : s) {
boolean x = b.f(t, 5);
if (x) {
r = r + z;
}
}
return r;
}
// main added by myself for testing purpose
public static void main(String[] args){
A test = new A();
test.s();
}
}
interface B{
int t();//complex calculus
boolean f(int a, int b); // complex algorithm
}
我认为,该错误与以下内容有关:
s(B b)
与属性同名的方法s
或interface B
未实施
非常感谢您的帮助!
解决方案
您可以查看此代码以供参考:
import java.util.List;
import java.util.ArrayList;
public class A implements B{
private List<String> s;
public A() {
s = new ArrayList<String>();
s.add("Bob");
s.add("Alice");
s.add("Eve");
}
//overridden method from interface B
public int t(){
//what you want this method to do
}
//overridden method from interface B
public boolean f(int a, int b){
//what you want this method to do
}
public String s(){
A test = new A();
int t = test.t();
String r = "Hello ";
for (String z : s) {
boolean x = test.f(t, 5);
if (x) {
r = r + z;
}
}
return r;
}
// main added by myself for testing purpose
public static void main(String[] args){
A test = new A();
System.out.println(test.s()); //printing the result of `test.s()`
}
}
interface B{
int t();//complex calculus
boolean f(int a, int b); // complex algorithm
}
您可以参考此链接以进一步了解。
推荐阅读
- android - 如何找到我的 URL 以连接到 Firebase?
- python - 如何附加到列表列表中的列表?
- swift - Passing JSON as a parameter swift
- node.js - CSS 文件未通过 <% include file.ejs %> 链接
- typescript - firebase cloud function is not being executed on onCreate event for firebase real time database
- php - 错误。laravel 加载 pdf 文件失败
- python-3.x - 如何从 python 中的 watson 助手读取插槽
- arrays - 如何将数组发布到 mvc 控制器
- php - Database Connection error using sql server and codeigniter in IIS
- python-3.x - Unable to read MS SQL table using pyspark in jupyter notebook?