Delphi属性读/写
发布时间:2020-12-15 04:23:41 所属栏目:大数据 来源:网络整理
导读:在delphi类中声明属性时,是否可能有不同类型的结果? 例: property month:string read monthGet(string)write monthSet(integer); 在这个例子中,我希望,在财产月份,当我: 读,我得到一个字符串; SET,我设置一个整数; 解决方法 最接近的是使用 Operator Ove
在delphi类中声明属性时,是否可能有不同类型的结果?
例: property month:string read monthGet(string)write monthSet(integer); 在这个例子中,我希望,在财产月份,当我: 解决方法
最接近的是使用
Operator Overloading,但Getter / Setter必须是同一类型.没有办法改变这一点.
program so_26672343; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type TMonth = record private FValue: Integer; procedure SetValue( const Value: Integer ); public class operator implicit( a: TMonth ): string; class operator implicit( a: Integer ): TMonth; property Value: Integer read FValue write SetValue; end; TFoo = class private FMonth: TMonth; public property Month: TMonth read FMonth write FMonth; end; { TMonth } class operator TMonth.implicit( a: TMonth ): string; begin Result := 'Month ' + IntToStr( a.Value ); end; class operator TMonth.implicit( a: Integer ): TMonth; begin Result.FValue := a; end; procedure TMonth.SetValue( const Value: Integer ); begin FValue := Value; end; procedure Main; var LFoo: TFoo; LMonthInt: Integer; LMonthStr: string; begin LFoo := TFoo.Create; try LMonthInt := 4; LFoo.Month := LMonthInt; LMonthStr := LFoo.Month; finally LFoo.Free; end; end; begin try Main; except on E: Exception do Writeln( E.ClassName,': ',E.Message ); end; end. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |