• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Delphi数据库面向对象编程范例_Delphi

Delphi 搞代码 3年前 (2018-08-09) 137次浏览 已收录 0个评论

{*******************************************************}
{ }
{ 继承范例 }
{ }
{ }
{ 版权所有 (C) 2000,2001 真光软件 }
{*******************************************************}
unit clsPerson;
{*******************************************************
项目:

http://www.gaodaima.com/?p=65751Delphi数据库面向对象编程范例_Delphi

模块: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.

欢迎大家阅读Delphi数据库面向对象编程范例_Delphi》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址