C
czj_earth
Unregistered / Unconfirmed
GUEST, unregistred user!
功能:
声明一个接口IMeter, 类TLot1RecordClass 实现接口,
全局函数GetMeterDataInterface返回创建的对象,并以(IMeter接口形式返回)
uMain单元中调用GetMeterDataInterface创建对象,为属性赋值
obj将新创建的对象转型为: TLot1RecordClass类,调用属性值, 返回值不正确.
why?
//接口声明单元
unit uMeterInteface
type
IMeter = interface //电表(种类不同,各种电表必须遵守和实现本接口)接口申明
['{98A7CE54-29FD-4093-8B2D-0625AE49072D}']
function GetMeterNO: Integer;
procedure SetMeterNO(Value: Integer);
property MeterNO: Integer read GetMeterNO write SetMeterNO;
....
end;
function GetMeterDataInterface: IMeter;
implementation
uses ulot1
function GetMeterDataInterface: IMeter;
begin
Result := Tlot1Recordclass.Create;
end;
uLot1
TLot1RecordClass = class(TInterfacedObject, IMeter)
private
FmeterNo: integer;
...
procedure SetMeterNO(Value: integer);
function GetMeterNO: integer;
public
property MeterNO: integer read GetMeterNO write SetMeterNO
end;
function TLot1RecordClass.GetMeterNO: integer;
begin
result := FMETERNO;
end;
procedure TLot1RecordClass.SetMeterNO(Value: integer);
begin
FMETERNO := Value;
end;
uMain //测试单元
private
FILot1: IMeter;
...
procedure TForm1.Button2Click(Sender: TObject);
var
obj: TLot1RecordClass;
begin
FILot1:= GetMeterDataInterface
//接口对象
if FILot1 <> nil then
begin
FILot1.MeterNO:= 1000;
showmessage(Inttostr(FILot1.MeterNo))
//显示1000
obj:= TLot1RecordClass(FILot1);
showmessage(Inttostr(obj.MeterNo)); //显示不确定数
MeterObject.IIMeter:= FILot1;
end;
end;
声明一个接口IMeter, 类TLot1RecordClass 实现接口,
全局函数GetMeterDataInterface返回创建的对象,并以(IMeter接口形式返回)
uMain单元中调用GetMeterDataInterface创建对象,为属性赋值
obj将新创建的对象转型为: TLot1RecordClass类,调用属性值, 返回值不正确.
why?
//接口声明单元
unit uMeterInteface
type
IMeter = interface //电表(种类不同,各种电表必须遵守和实现本接口)接口申明
['{98A7CE54-29FD-4093-8B2D-0625AE49072D}']
function GetMeterNO: Integer;
procedure SetMeterNO(Value: Integer);
property MeterNO: Integer read GetMeterNO write SetMeterNO;
....
end;
function GetMeterDataInterface: IMeter;
implementation
uses ulot1
function GetMeterDataInterface: IMeter;
begin
Result := Tlot1Recordclass.Create;
end;
uLot1
TLot1RecordClass = class(TInterfacedObject, IMeter)
private
FmeterNo: integer;
...
procedure SetMeterNO(Value: integer);
function GetMeterNO: integer;
public
property MeterNO: integer read GetMeterNO write SetMeterNO
end;
function TLot1RecordClass.GetMeterNO: integer;
begin
result := FMETERNO;
end;
procedure TLot1RecordClass.SetMeterNO(Value: integer);
begin
FMETERNO := Value;
end;
uMain //测试单元
private
FILot1: IMeter;
...
procedure TForm1.Button2Click(Sender: TObject);
var
obj: TLot1RecordClass;
begin
FILot1:= GetMeterDataInterface
//接口对象
if FILot1 <> nil then
begin
FILot1.MeterNO:= 1000;
showmessage(Inttostr(FILot1.MeterNo))
//显示1000
obj:= TLot1RecordClass(FILot1);
showmessage(Inttostr(obj.MeterNo)); //显示不确定数
MeterObject.IIMeter:= FILot1;
end;
end;