Delphi数据库面向对象编程范例
2004-5-15 2:17:17 (文章类别:Delphi)
{*******************************************************}
{ }
{ 继承范例 }
{ }
{ }
{ 版权所有 (C) 2000,2001 真光软件 }
{*******************************************************}
unit clsPerson;
{*******************************************************
项目:
模块:TPerson,TTeacher,TStudent
描述:OOP范例类
版本:1.0
日期:2001.9.23
作者:黄洪烈
更新:2001.9.23
TODO:
*******************************************************}
interface
uses
?clsApplication,Forms,classes,Dialogs;
const
?C_tblStudent='student.db';//学生类表名
?C_tblTeacher='teacher.db';//老师类表名
type
?TPerson=class(TDbRecordset)
?private
??FTelephone: string;
??FId: string;
??FName: string;
??function GetId: string;
??function GetName: string;
??function GetTelephone: string;
?protected
?? procedure BindFieldvalues;virtual;//设置字段值
?public
??property Id:string read GetId write FId;
??property Name:string read GetName write FName;
??property Telephone:string read GetTelephone write FTelephone;
??procedure ShowId;//显示编码
?end;
?TStudent=class(TPerson)
?private
??FTotalScore: integer;
??function GetTotalScore: integer;
?protected
??procedure BindFieldvalues;override;//设置字段值
?public
??constructor Create(AOwner:TComponent);override;
??function Post:boolean;override;
??property TotalScore:integer read GetTotalScore write FTotalScore;
?end;
?TTeacher=class(TPerson)
?private
??FSubject: string;
??function GetSubject: string;
?protected
??procedure BindFieldvalues;override;//设置字段值
?public
??constructor Create(AOwner:TComponent);override;
??function Post:boolean;override;
??property Subject:string read GetSubject write FSubject;
?end;
implementation
{ TPerson }
{设置字段值}
procedure TPerson.BindFieldvalues;
begin
?FTable.Edit;
?FTable.FieldByName('Id').AsString:=FId;
?FTable.FieldByName('Name').AsString:=FName;
?FTable.FieldByName('Telephone').AsString:=FTelephone;
end;
{定义字段属性}
function TPerson.GetId: string;
begin
?FId:=FTable.FieldByName('Id').AsString;
?Result:=FId;
end;
function TPerson.GetName: string;
begin
?FName:=FTable.FieldByName('Name').AsString;
?Result:=FName;
end;
function TPerson.GetTelephone: string;
begin
?FTelephone:=FTable.FieldByName('Telephone').AsString;
?Result:=FTelephone;
end;
{显示编码}
procedure TPerson.ShowId;
var
?IdCaption,Id:string;
begin
?Id:=FTable.FieldByName('Id').AsString;
?if FTable.TableName=C_tblStudent then
??IdCaption:='Student Id:'
?else
if FTable.TableName=C_tblTeacher then
??IdCaption:='Teacher Id:';
?ShowMessage(IdCaption+Id);
end;
{ TStudent }
{设置字段值}
procedure TStudent.BindFieldvalues;
begin
?inherited;
?FTable.FieldByName('TotalScore').AsInteger:=FTotalScore;
end;
constructor TStudent.Create(AOwner: TComponent);
begin
?inherited;
?FTable.TableName:=C_tblStudent;
?FTable.Active:=True;
end;
{定义字段属性}
function TStudent.GetTotalScore: integer;
begin
?FTotalScore:=FTable.FieldByName('TotalScore').AsInteger;
?Result := FTotalScore;
end;
{保存记录}
function TStudent.Post: boolean;
begin
?FTable.Insert;
?BindFieldvalues;
?Result:=Inherited Post;
end;
{ TTeacher }
{设置字段值}
procedure TTeacher.BindFieldvalues;
begin
?inherited;
?FTable.FieldByName('Subject').AsString:=FSubject;
end;
constructor TTeacher.Create(AOwner: TComponent);
begin
?inherited;
?FTable.TableName:=C_tblTeacher;
?FTable.Active:=True;
end;
{定义字段属性}
function TTeacher.GetSubject: string;
begin
?FSubject:=FTable.FieldByName('Subject').AsString;
?Result := FSubject;
end;
{保存记录}
function TTeacher.Post: boolean;
begin
?FTable.Insert;
?BindFieldvalues;
?Result:=Inherited Post;
end;
end.