缓存实例:
class person{ private static int Max_Size=10; private static person[] cache=new person[Max_Size]; public static int pos=0; private final String Name; private person(String name){ this.Name=name; } public String getName(){ return this.Name; } public static person valueOf(String name){ for(int i=0;i<Max_Size;i++){ if(cache[i]!=null&&cache[i].getName().equals(name)){ return cache[i]; } } if(pos==Max_Size){ cache[0]=new person(name); pos=1;
return cache[pos-1]; } else{ cache[pos++]=new person(name);
return cache[pos-1]; }
} public boolean equals(Object obj){ if(this==obj){ return true; } if(obj!=null &&obj.getClass()==person.class){ person t=(person)obj; return Name.equals(t.getName()); } return false; } } public class test { public static void main(String args[]){ person p1=person.valueOf("lili"); person p2=person.valueOf("leilei"); person p3=person.valueOf("lili"); System.out.println(person.pos); } }