前言
虚函数的作用,用专业术语来解释就是实现多态性(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 variablesforward 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 prototypespublic function integer uf_save ();return 0
end functionpublic subroutine uf_preview (integer ai_fs);//ai=0 是预览 ai=1 是打印
end subroutinepublic subroutine uf_init ();//装入数据窗口
end subroutine
public subroutine uf_setxgncs (integer ai_i);//修改年初数
end subroutine
public subroutine uf_help ();
end subroutinepublic subroutine uf_add ();//
end subroutinepublic subroutine uf_delete ();//
end subroutinepublic 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 functionpublic 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 ifend 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 subroutinepublic 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里的方法