interfaces
上午完成thinking Java中關于Interfaces章節的內容。下面是該章節中關于"Interfaces and factories"的例子
package com.zhanjh.thinkingjava.interfaces;
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
public Implementation1() {
// TODO Auto-generated constructor stub
}
public void method1(){
System.out.println("Implementation1 method1");
}
public void method2(){
System.out.println("Implementation1 method2");
}
}
class Implementation1Factory implements ServiceFactory{
public Service getService(){
return new Implementation1();
}
}
class Implementation2 implements Service{
public Implementation2() {
// TODO Auto-generated constructor stub
}
public void method1(){
System.out.println("Implementation2 method1");
}
public void method2(){
System.out.println("Implementation2 method2");
}
}
class Implementation2Factory implements ServiceFactory{
public Service getService(){
return new Implementation2();
}
}
public class Factories{
public static void serviceConsumer(ServiceFactory fact){
Service s=fact.getService();
s.method1();
s.method2();
}
public static void main(String[] args){
serviceConsumer(new Implementation1Factory());
serviceConsumer(new Implementation2Factory());
}
}
總結:abstract class和interface是Java語言中對于抽象類定義進行支持的兩種機制,abstract class和interface之間在對于抽象類定義的支持方面具有很大的相似性。目前我對他們區分的方法大致如下:
1)interface可以多重實現,而abstract class只能單一繼承
2)abstract class不一定只有抽象的方法(abstract method),它也可以包含具體的方法(concrete method)。而interface不能包含方法的實現(implementation)。所以在程序設計的時候,能用inteface的時候盡量不要用abstract class。
下午
查找關于EJB的資料,沒頭緒。
jaxb入門學習。
xjc(將xsd文件轉換為Java的小工具)工具的使用。可以創建一個bat文件處理下面的命令:
xjc -d "D:"eclipse"workspace"JaxbTest"src" -p "edu.jlu.xml" "D:"eclipse"workspace"JaxbTest"schema"messages.xsd"
其中D:"eclipse"workspace"JaxbTest"src為原文件的目錄,edu.jlu.xml為生成Java類的包名,D:" eclipse"workspace"JaxbTest"schema"messages.xsd為xml schema文件的路徑。
posted on 2007-12-26 19:07
zhan 閱讀(220)
評論(0) 編輯 收藏