用對象編程中虛函數技術解決PB難題

來源:互聯網
上載者:User

  前言

      虛函數的作用,用專業術語來解釋就是實現多態性(Polymorphism),多態性是將介面與實現進行分離;用形象的語言來解釋就是實現以共同的方法,但因個體差異而採用不同的策略。多態還有個關鍵之處就是一切用指向基類的指標或引用來操作對象。在C++和Java中,對虛函數的使用得心應手,抽象類別只聲明不實現,具體實現在子類中,

可通過子類執行個體賦給父物件的方法調用子類方法。但在PB中如何?呢?

1 建立父物件u_main,並建立虛函數(不實現)和執行個體變數

global type u_main from userobject
end type
boolean ib_zdjs=false
//當使用者需要修改年初數時進行賦值
boolean ib_ncs=false
//資料視窗是否已修改
boolean ib_modify=false
//當前報表的文書序號
string is_wsxh
end variables

forward prototypes
public function integer uf_save ()
public subroutine uf_preview (integer ai_fs)
public subroutine uf_init ()
public subroutine uf_setxgncs (integer ai_i)
public subroutine uf_help ()
public subroutine uf_add ()
public subroutine uf_delete ()
public subroutine uf_js ()
end prototypes

public function integer uf_save ();return 0
end function

public subroutine uf_preview (integer ai_fs);//ai=0  是預覽  ai=1 是列印
end subroutine

public subroutine uf_init ();//裝入資料視窗

end subroutine

public subroutine uf_setxgncs (integer ai_i);//修改年初數

end subroutine

public subroutine uf_help ();
end subroutine

public subroutine uf_add ();//
end subroutine

public subroutine uf_delete ();//
end subroutine

public subroutine uf_js ();//
end subroutine

 

二  繼承父物件u_main產生多個對象,並各自對父物件的虛函數進行實現
             global u_dk003_lr u_dk003_

public function integer uf_save ()
public subroutine uf_init ()
public subroutine uf_preview (integer ai_fs)
public subroutine uf_help ()

public function integer uf_save ();dw_1.update()
return 1
end function

public subroutine uf_init ();long ll_row
int i
dw_1.settransobject(sqlca)
ll_row=dw_1.retrieve()
if ll_row=0 then
 ll_row=dw_1.insertrow(0) 
end if

end subroutine

public subroutine uf_preview (integer ai_fs);if ai_fs=0 then
 OpenWithParm( w_dw_print_ws , "d_dk003_print")
end if
if ai_fs=1 then  //  列印
   dw_print.dataobject="d_dk003_print"
 dw_print.settransobject(sqlca)
 dw_print.retrieve()
 end if
 
end subroutine

public subroutine uf_help ();OpenWithParm(w_help,"DK003")
end subroutine

三、使用u_main變數,建立多個執行個體變數,靈活編程

          u_main uo_1  

          uo_1=create u_dk003_lr   //建立子物件執行個體

         uo_1.uf_init()  //啟動並執行是子物件u_dk003_lr裡的方法

         uo_1=create u_dk004_lr   //建立子物件執行個體

         uo_1.uf_init()  //啟動並執行是子物件u_dk004_lr裡的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.