简体   繁体   English

如何从另一个类调用HashMap的方法?

[英]How to call the method of HashMap from another class?

In my assessment class, I have a method called addMark and it puts both studentid and mark into the hashmap called marks. 在我的评估课中,我有一个名为addMark的方法,它将学生ID和标记都放入称为标记的哈希图中。 I'm trying to put studentid and mark into the hashmap using student class but it keeps on giving me errors. 我正在尝试使用学生类将studentid和mark放入哈希图中,但它一直在给我错误。 Am I calling the assessment class wrongly? 我打错了评估班吗? Can someone please help? 有人可以帮忙吗?

Student class: 学生班:

public class Student {
 public void createStudents() {
   Mark m1 = new Mark(30, "Bad");
   Assessment.addMark("11", m1);
}

Mark class: 标记等级:

public class Mark {

private int mark;
private String comment;

public Mark(int mark){

}

public Mark(int mark, String comment){

}

public void setMark(int mark) throws Exception {
    /*
     * Start Preconditions
     */
    // Precondition: firstArgumentNonNegative
    if (mark < 0){
        throw new Exception("Precondition violated:"
                + " firstArgumentNonNegative");
    }
    /*
     * End Preconditions
     */
    this.mark = mark;
}

public int getMark() {
    return this.mark;
}

public void setComment(String comment){
    this.comment = comment;
}

public String getComment(){
    return this.comment;
}
}

Assessment class: 评估课:

import java.util.HashMap;
import java.util.Map;

public abstract class Assessment {
private Map<String, Mark> marks = new HashMap<String, Mark>();


public String getAllStudentMarks(){
    String outString = "Student ID:\tMark:\tComment:";
    for (Map.Entry<String, Mark> entry : marks.entrySet()){
        String studentId = entry.getKey();
        Mark mark = entry.getValue();
        int value = mark.getMark();
        String comment = mark.getComment();
        outString += String.format("\n%s\t%d\t%s", studentId, value, comment);
    }
    return outString;
}

public boolean hasCompleted(String studentId){
    return marks.containsKey(studentId);
}


public void addMark(String studentId, Mark mark) throws Exception{
    /*
     * Start Preconditions
     */
    // Precondition: markNotGreaterThanWeight
    if (mark.getMark() > this.weight){
        throw new Exception("Precondition violated: "
                + "markNotGreaterThanWeight");
    }
    /*
     * End Preconditions
     */
    marks.put(studentId, mark);
}

public int getMark(String studentId){
    return marks.get(studentId).getMark();
}

public String getComment(String studentId){
    return marks.get(studentId).getComment();
}   

public abstract String description();
}

Assessment is an abstract class so you will need to extend it if you want to use it: Assessment是一个抽象类,因此如果要使用它,则需要对其进行扩展:

class MyAssessment extends Assessment {
    @Override
    public String description() {
       return "My assessment";
    } 
}

Then you can call addMark on this new implementation 然后,您可以在此新实现上调用addMark

MyAssessment myAssessment = new MyAssessment();
myAssessment.addMark("11", m1);

Also this method throws an Exception so you need to try/catch it 另外,此方法会引发Exception因此您需要尝试/捕获它

try {
    new MyAssessment().addMark("11", m1);
} catch(Exception e) {
    e.printStackTrace();
}

And finally you have no weight property in your Assessment class so you should somehow define one if you want the class to compile. 最后,您的Assessment类中没有weight属性,因此,如果要编译该类,应该以某种方式定义一个。

private int weight = 0;

I'm not able to get the exact question but through your code, I found. 我找不到确切的问题,但是通过您的代码,我发现了。

  1. You need to extend abstract class Assessment ex. 您需要扩展抽象类Assessment ex。 AssessmentManager

     class AssessmentManager extends Assessment 
  2. Create an object of AssessmentManager and then use it. 创建AssessmentManager对象,然后使用它。

     AssessmentManager _assessment = new AssessmentManager(); _assessment.addMark(Student_ID, Marks) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM