delphi – 使用数组重载运算符
发布时间:2020-12-15 09:05:22 所属栏目:大数据 来源:网络整理
导读:我有这个单位: unit Main.TIns;interfacetype TIns = record private type TInsArray = array [0..90] of Integer; var FInsArray: TInsArray; public class operator Implicit(const Value: Integer): TIns; class operator Add(const Elem1: TIns; const
我有这个单位:
unit Main.TIns; interface type TIns = record private type TInsArray = array [0..90] of Integer; var FInsArray: TInsArray; public class operator Implicit(const Value: Integer): TIns; class operator Add(const Elem1: TIns; const Elem2: Integer): TIns; end; implementation class operator TIns.Implicit(const Value: Integer): TIns; var iIndex: Integer; begin if Value = 0 then for iIndex := 0 to 90 do Result.FInsArray[iIndex] := 0; end; class operator TIns.Add(const Elem1: TIns; const Elem2: Integer): TIns; begin Inc(Result.FInsArray[0]); Result.FInsArray[Result.FInsArray[0]] := Elem2; end; end. 主要程序是: program InsMain; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils,Main.TIns in 'Main.TIns.pas'; var s: TIns; begin s := 0; // Initialize ins; similar t := [] // s := s + 5; // Add a element,in this case 5 // writeln(s[0]); // Read number of element in s,should to be 1 // end. 问题是我收到此错误:[DCC错误] InsMain.dpr(20):E2149类没有默认属性. type TIns = record private type TInsArray = array [0..90] of Integer; var FInsArray: TInsArray; public MyVal: TInsArray; class operator Implicit(const Value: Integer): TIns; class operator Add(const Elem1: TIns; const Elem2: Integer): TIns; end; 然后我修改添加所以: class operator TIns.Add(const Elem1: TIns; const Elem2: Integer): TIns; begin Inc(Result.FInsArray[0]); Result.FInsArray[Result.FInsArray[0]] := Elem2; MyVal := Result; // <--- error E2124 here. end; 和写作: writeln(s.MyVal[0]); 不返回错误,但在添加时给出错误,写入:[DCC错误] Main.TIns.pas(31):E2124实例成员’MyVal’在这里无法访问,所以不明白我错在哪里. 解决方法
以下是您尝试执行的操作示例:
type TIns = record private type TInsArray = array [0..90] of Integer; private var FInsArray : TInsArray; function GetItem( index : integer) : integer; function GetCount : integer; public class operator Implicit(const Value: Integer): TIns; class operator Add(const Elem1: TIns; const Elem2: Integer): TIns; property Items[index : integer] : integer read GetItem; default; property Count : integer read GetCount; end; function TIns.GetCount: integer; begin Result := Self.FInsArray[0]; end; function TIns.GetItem(index: integer): integer; begin Result := Self.FInsArray[index]; end; class operator TIns.Implicit(const Value: Integer): TIns; var iIndex: Integer; begin if Value = 0 then for iIndex := 0 to 90 do Result.FInsArray[iIndex] := 0; end; class operator TIns.Add(const Elem1: TIns; const Elem2: Integer): TIns; begin Result := Elem1; Inc(Result.FInsArray[0]); Result.FInsArray[Result.FInsArray[0]] := Elem2; end; var i : integer; s,s1 : TIns; begin s := 0; // Initialize ins; similar t := [] // s1 := 0; s := s + 5; // Add a element,in this case 5 // s1 := s1 + 10; for i := 1 to s.Count do writeln( 'S[',i,']=',s[i]); // Read element values in s for i := 1 to s1.Count do writeln( 'S1[',s1[i]); // Read element values in s1 ReadLn; end. 要提取数组元素,请声明默认属性Items.通过属性Count公开元素计数.正如Uwe指出的那样,在Add运算符中首先将Result设置为Elem1. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- django 配置media文件,处理上传图片/文件方法设置
- Lua包(代码文件的组织和调用)_类_继承_多态
- CryENGINE3初探Entities (一)----使用Lua创建一个自定义Ent
- 如何在Spring Framework中设置BasicDataSource的最大池大小
- 使用ClassHelper\record helper 给类“打补丁”
- Lua的语法缺失及替代措施
- 征服Perl――子程序等相关知识――里程碑M5
- 直方图相似性度量方式(一) EMD
- Lua: attempt to call method 'xxxx' (a nil va
- Practice in groovy