一些基礎鞏鞏固

1)回調

jndi源碼回調

public Object lookup(final String name) {

return execute(new JndiCallback<Object>() {

public Object doInContext(Context ctx) throws NamingException {

Object located = ctx.lookup(name);

return located;

} });  }



public <T> T execute(JndiCallback<T> contextCallback) throws NamingException {

Context ctx = getContext();

try {return contextCallback.doInContext(ctx);}

finally {releaseContext(ctx); }}


2) 枚舉

public static enum ModelCategoryType{

V("V",1),AFA_P("AFA-P",2),AFA_N("AFA-N",3),U("U",4);

private final int priority;

private final String name;

ModelCategoryType(String name,int priority){

this.name=name;

this.priority=priority;

}

public int getPriority() {

return priority;

}

public String getName() {

return name;

}

public static int getPriorityFromName(String name){

if(name!=null){

for(ModelCategoryType modelCategoryType :ModelCategoryType.values()){

if(name.equals(modelCategoryType.getName())){

return modelCategoryType.getPriority();

}

}

}

return Integer.MAX_VALUE;

}

}




String newMdlCategory=RubyConstants.ModelCategoryType.V.getName();

RubyConstants.ModelCategoryType.getPriorityFromName(newMdlCategory)

<(RubyConstants.ModelCategoryType.getPriorityFromName(originalMdlCategory))


3) deep clone

/**

 * This method makes a "deep clone" of any Java object it is given.

 */

 public static Object deepClone(Object object) {

   try {

     ByteArrayOutputStream baos = new ByteArrayOutputStream();

     ObjectOutputStream oos = new ObjectOutputStream(baos);

     oos.writeObject(object);

     ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

     ObjectInputStream ois = new ObjectInputStream(bais);

     return ois.readObject();

   }

   catch (Exception e) {

     e.printStackTrace();

     return null;

   }

 }

// (1) create a Person object named Al

Address address = new Address("305 West Fern Avenue", "Palmer", "Alaska");

Person al = new Person("Al", "Alexander", address);


// (2) make a deep clone of Al

Person neighbor = (Person)deepClone(al);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章