posts - 6,  comments - 7,  trackbacks - 0
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 阅读(205) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2007年12月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(1)

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜