关于类的Create方法等问题(100分)

  • 主题发起人 主题发起人 szsunboy
  • 开始时间 开始时间
S

szsunboy

Unregistered / Unconfirmed
GUEST, unregistred user!
请各位大虾看一下下面我所定的类:
1.我想在创建时,输入两个参数:FileName、FieldNo(现在只让我输入一下)
2.我想在类创建时,就自动设置其中的属性值
如果您能将我的类完整地改写一下,并发给我(newemail@163.net),让本人来认真学习一下,我将不胜感激!

另外:
这个类是要实现这样的一个功能:
从.ini文件里面读到一个Person定义(根据No),并给其赋初值
文件格式为:
[111]
NAME=Tom
SEX=F
AGE=10

以下是我的类:
---------------------------------------------------------------
unit PersonDef;

interface
uses
Classes, SysUtils, Windows, IniFiles, Forms;

type
TPerson = Class(TIniFile)
private
fNo: Integer;

function GetName: Variant;
function GetSex: String;
function GetAge: String;
protected
constructor Create(const No: Integer
FileName: String);override;
destructor Destroy;
public
property Name: String Read GetName;
property Sex: String Read GetSex;
property Age: Integer Read GetAge;
end;

implementation

constructor Create(const No: Integer
FileName: String);override;
begin
fNo := 0;
inherited Create(FileName);
end;

destructor Destroy;
begin
inherited Destory;
end;

function TPerson.GetName: Variant;
var
SecName: String;
begin
SecName := Format('%.3d', [fNo]);
Result := ReadString(SecName, 'NAME', '');
end;

function TPerson.GetSex: String;
var
SecName: String;
begin
SecName := Format('%.3d', [fNo]);
Result := ReadString(SecName, 'SEX', '');
end;

function TPerson.GetAge: Integer;
var
SecName: String;
begin
SecName := Format('%.3d', [fNo]);
Result := ReadString(SecName, 'AGE', 'C');
end;

end.
 
unit PersonDef;

interface
uses
Classes, SysUtils, Windows, IniFiles, Forms;

type
TPerson = Class(TIniFile)
private
fNo: Integer;

function GetName: string;
function GetSex: String;
function GetAge: integer;
protected
constructor Create(const No: Integer
FileName: String);{改}
destructor Destroy;
public
property Name: String Read GetName;
property Sex: String Read GetSex;
property Age: Integer Read GetAge;
end;

implementation

constructor TPerson.Create(const No: Integer
FileName: String);
begin
fNo := No;
inherited Create(FileName);
end;

destructor TPerson.Destroy;
begin
inherited
end;

function TPerson.GetName: string;
var
SecName: String;
begin
SecName := Format('%.3d', [fNo]);
Result := ReadString(SecName, 'NAME', '');
end;

function TPerson.GetSex: String;
var
SecName: String;
begin
SecName := Format('%.3d', [fNo]);
Result := ReadString(SecName, 'SEX', '');
end;

function TPerson.GetAge: Integer;
var
SecName: String;
begin
SecName := Format('%.3d', [fNo]);
Result := StrToInt(ReadString(SecName, 'AGE', 'C'));
end;

end.

试试这个,若不行,可与我联系,wenfei0203@sina.com
 
你出现的问题应该是由于把建构函数放到PROTECTED中
可以
TPerson = Class(TIniFile)
private
fNo: Integer;

function GetName: Variant;
function GetSex: String;
function GetAge: String;
public
constructor Create(const No: Integer
FileName: String);
property Name: String Read GetName;
property Sex: String Read GetSex;
property Age: Integer Read GetAge;
end;
 
寫具體一點吧!
 
unit Unit2;

interface
uses
Classes, SysUtils, Windows, IniFiles, Forms;

type
TPerson = Class(TIniFile)
private
fNo: Integer;

function GetName: String;
function GetSex: String;
function GetAge: integer;
protected
public
constructor Create(const No: Integer
FileName: String);
destructor Destroy;
property Name: String Read GetName;
property Sex: String Read GetSex;
property Age: Integer Read GetAge;
end;

implementation

constructor TPerson.Create(const No: Integer
FileName: String);
begin
fNo := 0;
inherited Create(FileName)
//注意,这行可能有问题,你试试,不行的话,要将TINIFILE的CREATE代码写在这儿
end;

destructor TPerson.Destroy;
begin
inherited;
end;

function TPerson.GetName: string;
var
SecName: String;
begin
SecName := Format('%.3d', [fNo]);
Result := ReadString(SecName, 'NAME', '');
end;

function TPerson.GetSex: String;
var
SecName: String;
begin
SecName := Format('%.3d', [fNo]);
Result := ReadString(SecName, 'SEX', '');
end;

function TPerson.GetAge: Integer;
var
SecName: String;
begin
SecName := Format('%.3d', [fNo]);
Result := strtoint(ReadString(SecName, 'AGE', 'C'));
end;

end.
 
后退
顶部