簡體   English   中英

Java - 在類定義中找不到錯誤

[英]Java - Can't find error in class definition

我對以下代碼有問題。 這是一項考試任務,我無法弄清楚給定代碼中的錯誤是什么。 我們應該在 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
}

我認為,該錯誤與以下內容有關:

  1. 與屬性s同名的方法s(B b)
  2. 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
}

您可以參考此鏈接進一步了解。

來源: https : //www.geeksforgeeks.org/interfaces-in-java/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM