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

请将查找目标的过程定义为类方法,查找时直接调用类方法

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);}

临摹微笑
浏览 91回答 1
1回答

catspeake

我把方法改成非静态的了.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);}}}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
JavaScript