package model;

 

public class DemoDAO {

    public DemoDAO() {

       System.out.println("DemoDAO's constructor");

    }

}






範例一:

package model;

 

public class DemoService {

private DemoDAO dao =new DemoDAO();

}

 

 


範例一:

package org.iiiedu.samuel.ProjSpringDemo;

 

import model.DemoService;

 

public class TestDemoService {

 

    public static void main(String[] args) {

       DemoService service = new DemoService();

 

    }

 

}





範例二:

package model;

 

public class DemoService {

private DemoDAO dao =null;

public DemoService(DemoDAO dao){

    //我是建構子

    this.dao=dao;

}

 

}



範例二:

 

package org.iiiedu.samuel.ProjSpringDemo;

 

import model.DemoDAO;

import model.DemoService;

 

public class TestDemoService {

 

    public static void main(String[] args) {

       DemoDAO dao = new DemoDAO();

       DemoService service = new DemoService(dao);

 

    }

 

}






範例三;

package model;

 

public class DemoService {

//範例二

//private DemoDAO dao =null;

//public DemoService(DemoDAO dao){

//  //我是建構子

//  this.dao=dao;

//}

    private DemoDAO dao = null;

    public void setDAO(DemoDAO dao) {

       // 我是建構子

       this.dao = dao;

    }

   

   

   

}




範例三;

package org.iiiedu.samuel.ProjSpringDemo;

 

import model.DemoDAO;

import model.DemoService;

 

public class TestDemoService {

 

    public static void main(String[] args) {

//     範例二

//     DemoDAO dao = new DemoDAO();

//     DemoService service = new DemoService(dao);

 

       DemoDAO dao = new DemoDAO();

       DemoService service = new DemoService();

       service.setDAO(dao);

 

      

    }

 

}

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 乙方 的頭像
    乙方

    學習筆記專區

    乙方 發表在 痞客邦 留言(0) 人氣()