无法从非静态方法调用到非静态类/方法。

huangapple go评论77阅读模式
英文:

Cannot access a method call from non-static to non-static class/method

问题

非常抱歉不得不因为这个愚蠢的问题而打扰大家,因为我现在应该真的知道这个,但还是来吧:

public class Toets {

    private String vak;

    /**
     * Constructor for objects of class Toets
     */
    public Toets(String toetsVak) {
        vak = toetsVak;
        new Vraag();
    }

    public void addVraag(int vraagID, String vraag, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord) {
        new Vraag(vraagID, vraag, antwoordA, antwoordB, antwoordC, correcteAntwoord);
    }
    
    public void printVraag(int vraagID) {
        Vraag.printVraag(vraagID);
    }
}

这应该从这里调用:

public class Vraag {

    private int vraagID;
    private String vraag;
    private String antwoordA;
    private String antwoordB;
    private String antwoordC;
    private String correcteAntwoord;    

    /**
     * Constructor for objects of class Vraag
     */
    public Vraag() {
        vraagID = 0;
        vraag = "";
        antwoordA = "";
        antwoordB = "";
        antwoordC = "";
        correcteAntwoord = "";
    }

    /**
     * Constructor for objects of class Vraag
     */
    public Vraag(int vraagID, String vraag, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord) {
        this.vraagID = vraagID;
        this.vraag = vraag;
        this.antwoordA = antwoordA;
        this.antwoordB = antwoordB;
        this.antwoordC = antwoordC;
        this.correcteAntwoord = correcteAntwoord;
    }
    
    public void printVraag(int vraagID) {
        if(this.vraagID == vraagID) {
            System.out.println("Vraag " + vraagID + ".");
            System.out.println(vraag);
            System.out.println("A. " + antwoordA);
            System.out.println("B. " + antwoordB);
            System.out.println("C. " + antwoordC);
        }
    }
}

我试图做的是为学校创建一个测试,并逐个添加问题,并根据指定的ID(即问题编号)打印问题;我们不允许使用静态方法,所以我已经查看了一些方法来实现这个,但是在Java.lang.reflection部分我有点困惑。

问题是我需要能够创建多个Vraag的实例,并调用每个实例的打印方法,但我现在显然无法做到这一点。

英文:

I am terribly sorry to have to bother you all with this silly question since I should really know this by now but here goes:

public class Toets
private String vak;
/**
* Constructor for objects of class Toets
*/
public Toets(String toetsVak)
{
vak = toetsVak;
new Vraag();
}
/**
* An example of a method - replace this comment with your own
*
* @param  y  a sample parameter for a method
* @return    the sum of x and y
*/
public void addVraag(int vraagID, String vraag, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord)
{
new Vraag(vraagID, vraag, antwoordA, antwoordB, antwoordC, correcteAntwoord);
}
public void printVraag(int vraagID)
{
Vraag.printVraag(vraagID);
}

which should be called from here:

public class Vraag
private int vraagID;
private String vraag;
private String antwoordA;
private String antwoordB;
private String antwoordC;
private String correcteAntwoord;    
/**
* Constructor for objects of class Vraag
*/
public Vraag()
{
vraagID = 0;
vraag = "";
antwoordA = "";
antwoordB = "";
antwoordC = "";
correcteAntwoord = "";
}
/**
* Constructor for objects of class Vraag
*/
public Vraag(int vraagID, String vraag, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord)
{
this.vraagID = vraagID;
this.vraag = vraag;
this.antwoordA = antwoordA;
this.antwoordB = antwoordB;
this.antwoordC = antwoordC;
this.correcteAntwoord = correcteAntwoord;
}
public void printVraag(int vraagID)
{
if(this.vraagID == vraagID)
{
System.out.println("Vraag " + vraagID + ".");
System.out.println(vraag);
System.out.println("A. " + antwoordA);
System.out.println("B. " + antwoordB);
System.out.println("C. " + antwoordC);
}
}

}

The thing I am trying to do is create a Test for a school and add questions one by one and print a question in accordance with the designated ID (AKA question number); we are not allowed to use statics so I have looked at ways to accomplish this but I kind of lose it at the Java.lang.reflection stuff.

The thing is that I need to be able to create multiple instances of Vraag and call the print method of each one which I apparently can not do at the moment.

答案1

得分: 0

代替将 Vraag 作为属性提供,只需提供 List<Vraag>

每当创建新的 Vraag 对象时,只需放入列表中。

在打印特定对象的所有属性时,您可以根据传递的 ID 进行记录过滤。

尝试以下代码,它将对您有所帮助。

class Toets {
    private String vak;
    private List<Vraag> vraagList;

    public Toets(String toetsVak) {
        vak = toetsVak;
        vraagList = new ArrayList<>(); // Initialize the list in the constructor
    }

    public void addVraag(int vraagID, String att, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord) {
        vraagList.add(new Vraag(vraagID, att, antwoordA, antwoordB, antwoordC, correcteAntwoord));
    }

    public void printVraag(int vraagID) {
        Vraag obj = vraagList.stream().filter(vraag -> vraag.getVraagID() == vraagID).findFirst().orElse(null);
        if (obj != null) {
            obj.printVraag(vraagID);
        }
    }
}

class Vraag {
    private int vraagID;
    private String vraag;
    private String antwoordA;
    private String antwoordB;
    private String antwoordC;
    private String correcteAntwoord;

    public Vraag(int vraagID, String vraag, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord) {
        this.vraagID = vraagID;
        this.vraag = vraag;
        this.antwoordA = antwoordA;
        this.antwoordB = antwoordB;
        this.antwoordC = antwoordC;
        this.correcteAntwoord = correcteAntwoord;
    }

    public void printVraag(int vraagID) {
        System.out.println("Vraag " + vraagID + ".");
        System.out.println(vraag);
        System.out.println("A. " + antwoordA);
        System.out.println("B. " + antwoordB);
        System.out.println("C. " + antwoordC);
        System.out.println("Correct antwoord: " + correcteAntwoord);
    }

    public int getVraagID() {
        return vraagID;
    }

    public void setVraagID(int vraagID) {
        this.vraagID = vraagID;
    }

    // Other getters and setters...
}

注意:我在代码中进行了一些修改,包括初始化 vraagList 列表,以及对于 printVraag 方法中未找到匹配对象的处理。

英文:

Instead of providing Vraag as attribute, you simple provide List&lt;Vraag&gt;.

Everytime when you create new Vraag object simply put into list.

And while printing all attributes of particular object you can filter out record based on passed Id.

Try below code it will help you.

class Toets {
private String vak;
private List&lt;Vraag&gt; vraagList;
public Toets(String toetsVak) {
vak = toetsVak;
}
public void addVraag(int vraagID, String att, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord) {
vraagList.add(new Vraag(vraagID, att, antwoordA, antwoordB, antwoordC, correcteAntwoord));
}
public void printVraag(int vraagID) {
//Java 8 code
Vraag obj=vraagList.stream().filter(vraag -&gt; vraag.getVraagID()==vraagID).collect(Collectors.toList()).get(0);
obj.printVraag(vraagID);
/* Before Java 8
for(Vraag obj:vraagList){
if(obj.getVraagID()==vraagID){
obj.printVraag(vraagID);
}
}*/
}
}
class Vraag {
private int vraagID;
private String vraag;
private String antwoordA;
private String antwoordB;
private String antwoordC;
private String correcteAntwoord;
public Vraag(int vraagID, String vraag, String antwoordA, String antwoordB, String antwoordC, String correcteAntwoord) {
this.vraagID = vraagID;
this.vraag = vraag;
this.antwoordA = antwoordA;
this.antwoordB = antwoordB;
this.antwoordC = antwoordC;
this.correcteAntwoord = correcteAntwoord;
}
public void printVraag(int vraagID) {
System.out.println(&quot;Vraag &quot; + vraagID + &quot;.&quot;);
System.out.println(vraag);
System.out.println(&quot;A. &quot; + antwoordA);
System.out.println(&quot;B. &quot; + antwoordB);
System.out.println(&quot;C. &quot; + antwoordC);
System.out.println(&quot;correcteAntwoord. &quot; + correcteAntwoord);
}
public int getVraagID() {
return vraagID;
}
public void setVraagID(int vraagID) {
this.vraagID = vraagID;
}
public String getVraag() {
return vraag;
}
public void setVraag(String vraag) {
this.vraag = vraag;
}
public String getAntwoordA() {
return antwoordA;
}
public void setAntwoordA(String antwoordA) {
this.antwoordA = antwoordA;
}
public String getAntwoordB() {
return antwoordB;
}
public void setAntwoordB(String antwoordB) {
this.antwoordB = antwoordB;
}
public String getAntwoordC() {
return antwoordC;
}
public void setAntwoordC(String antwoordC) {
this.antwoordC = antwoordC;
}
public String getCorrecteAntwoord() {
return correcteAntwoord;
}
public void setCorrecteAntwoord(String correcteAntwoord) {
this.correcteAntwoord = correcteAntwoord;
}
}

答案2

得分: 0

问题确实很难理解,但我猜想您想要一个用于存储这些问题的列表/数组或类似的东西,而且不能是静态的?

如果是这样的话,如果一个拥有该列表的类没有被多次创建,您就不需要一个静态(我猜您知道这是什么意思)对象。它拥有一个列表变量,但该变量只存在一次,我猜这正是您想要的。

英文:

Question is hard to decipher for sure, but I assume you want a list/array or something to store those questions in and that can't be static?

If so, you don't need a static (guess you know what that means) object if a class that has the list isn't created multiple times, it has a list variable but that variable only exists once which I assume is what you want

huangapple
  • 本文由 发表于 2020年8月17日 02:00:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63440283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定