为了账号安全,请及时绑定邮箱和手机立即绑定

大神!我的意思是把这个方法定义成类方法?请问该咋办?

大神!我的意思是把这个方法定义成类方法?请问该咋办?

临摹微笑 2022-06-01 11:07:04
请将查找目标的过程定义为类方法,查找时直接调用类方法import java.util.HashMap;public class HashMapExample{public static void main(String[] args) throws Exception{HashMap<String , student> students = new HashMap<String, student>();student s1 = new student("12345-12","AAA");student s2 = new student("98765-00","BBB");student s3 = new student("55667-99","CCC");students.put(s1.getIdNo(),s1);students.put(s2.getIdNo(),s2);students.put(s3.getIdNo(),s3);String id = "98765-00";System.out.println("Let's try to retrive a Student with ID = "+ id);student x = students.get(id);if (x!=null){System.out.println("Found! Name = "+ x.getName());}else {System.out.println("Invalid ID: "+ id);}System.out.println();id="00000-00";System.out.println("Let's try to retrive a Student with ID = "+ id);x=students.get(id);if (x!=null){System.out.println("Found! Name = "+x.getName());}else {System.out.println("Invalid ID: "+id);}System.out.println();System.out.println("Here are all of the student: ");System.out.println();for (student s : students.values() ){System.out.println("ID: "+s.getIdNo());System.out.println("Name: "+s.getName());System.out.println();}}}__________________________________________________________________public class student{private String name;private String idNo;public student(String i,String n){name=n;idNo=i;}public String getName() {return name;}public String getIdNo() {return idNo;}}String id = "98765-00";System.out.println("Let's try to retrive a Student with ID = "+ id);student x = students.get(id);if (x!=null){System.out.println("Found! Name = "+ x.getName());}else {System.out.println("Invalid ID: "+ id);}
查看完整描述

1 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

我把方法改成非静态的了.

package testmap;

import java.util.HashMap;

public class Test
{
static HashMap<String , Student> students = new HashMap<String, Student>();

public static void main(String[] args) throws Exception
{
new Test().go();

}

public void go(){

Student s1 = new Student("12345-12","AAA");
Student s2 = new Student("98765-00","BBB");
Student s3 = new Student("55667-99","CCC");

students.put(s1.getIdNo(),s1);
students.put(s2.getIdNo(),s2);
students.put(s3.getIdNo(),s3);

String id = "98765-00";
System.out.println("Let's try to retrive a Student with ID = "+ id);
Student x = students.get(id);
if (x!=null)
{
System.out.println("Found! Name = "+ x.getName());
}
else {
System.out.println("Invalid ID: "+ id);}

System.out.println();

id="00000-00";
System.out.println("Let's try to retrive a Student with ID = "+ id);
x=students.get(id);

if (x!=null)
{
System.out.println("Found! Name = "+x.getName());
}
else {
System.out.println("Invalid ID: "+id);}

System.out.println();
System.out.println("Here are all of the student: ");
System.out.println();

for (Student s : students.values() )
{
System.out.println("ID: "+s.getIdNo());
System.out.println("Name: "+s.getName());
System.out.println();

}
findById("00000-00");
findById("98765-00");

}

public static void findById(String id){
System.out.println("Let's try to retrive a Student with ID = "+ id);
Student x=students.get(id);

if (x!=null)
{
System.out.println("Found! Name = "+x.getName());
}
else {
System.out.println("Invalid ID: "+id);}
}
}

 


查看完整回答
反对 回复 2022-06-06
  • 1 回答
  • 0 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信