1。利用try.....catch ....進行邏輯轉換:

class Factory
{

public Factory()
{
}
public Title createTitle(String name)

{
Title title=null;

try
{
Title exiteTitle=get(name); //從數據庫中查找同名的Title,如果存在,拋出存在異常
throw new TitleAlreadyExistsException();


}catch(TitleNotFoundException e)
{
title =new Title();

}
}



public Forum getForum(String name) throws ForumNotFoundException
{ //查找數據庫,如果沒有找到,則拋出ForumNotFoundException 異常
return TitleManager.get(name);
}
}

2.對實現接口使用Adapter方式,不必實現所有接口方法
這樣我們就可直接使用接口中的某個方法而不比實現他的所有方法了
private static ClassLoader getTCL() throws IllegalAccessException,

InvocationTargetException
{

// Are we running on a JDK 1.2 or later system?
Method method = null;

try
{
method = Thread.class.getMethod("getContextClassLoader", null);

} catch (NoSuchMethodException e)
{
// We are running on JDK 1.1
return null;
}

return (ClassLoader) method.invoke(Thread.currentThread(), null);
}



































2.對實現接口使用Adapter方式,不必實現所有接口方法
1
public interface KeyListener extends SWTEventListener
{
2
3
public void keyPressed(KeyEvent e);
4
5
public void keyReleased(KeyEvent e);
6
}



2

3

4

5

6

1
public abstract class KeyAdapter implements KeyListener
{
2
public void keyPressed(KeyEvent e)
{
3
}
4
5
public void keyReleased(KeyEvent e)
{
6
}
7
}



2



3

4

5



6

7

這樣我們就可直接使用接口中的某個方法而不比實現他的所有方法了
1
button.addSelectionListener(new SelectionAdapter()
{
2
public void widgetSelected(SelectionEvent event)
{
3
handleSelectionEvent();
4
}
5
});



2



3

4

5

3.動態調用方法




















