对于Delphi 10.2.3:不确定如何问这个问题,但是如何创建类似于memo1.lines.add();的方法?和memo1.Lines.Count.ToString;行中包含许多其他方法,过程和属性?
这就是我的想法:
unit.someproperty.anotherproperty.mymethod(myvariable:variabletype):variabletype;
或
unit.someproperty.unit2.mymethod(myvariable:variabletype):variabletype;
外观如下:
function component.extract.tagA(attribute, source: string): string;
procedure component.insert.tagA(attribute, source, dest: string);
procedure component.modify.tagA(attribute, source, dest: string);
,如果您键入component。它将为您提供有关提取,插入和修改为下一步使用的选项的帮助。
所以,如何制作可以使用.extract的函数或过程。或.extract。或.insert。 ETC
我知道这应该是基础知识,但是我正在从事的项目变得越来越大,我可以更轻松地阅读和使用。我知道这是可以做到的,但是我不知道如何正确地用措辞才能找到我需要做的事情。
我想拥有多个单元...然后我使用它们创建一个组件,以便它们具有嵌套的方法和过程,这些方法和过程的工作方式与您在Tmemo中看到的DOTTED方法和过程一样...诸如memo1.lines.add,memo1。 lines.delete,memo1.lines.insert等。
请帮助!预先感谢!
答案 0 :(得分:3)
Delphi语法不允许您声明一个具有点缀子属性的类。
但是,您可以通过使用已知的编码样式来达到此目的 流畅的设计(请参见https://en.wikipedia.org/wiki/Fluent_interface)。这是一个非常简单的示例:
type
TMySubProperty = class
protected
procedure DoSomething;
property Width : Integer read {define getter and setter}
end;
TMyComponent = class(TComponent)
[...]
property
MySubProperty : TMySubProperty read GetMySubProperty;
end;
[...]
function TMyComponent.GetMySubProperty : TMySubProperty;
begin
Result := {calculate the result however you like}
end;
然后,您可以在代码中编写类似的内容
MyComponent.MySubProperty.Width := 666;
MyComponent.MySubProperty.DoSomething;
很显然,您可以具有任意数量的子属性,并且可以将它们嵌套到任意子属性中
级别:基本上,您需要一个用于子属性的类类型和一个拥有中的函数
类(在此简单示例中为TMyComponent),它返回您尝试的类实例
访问。可以对Getter
函数进行编码以接受标识特定参数的参数。
子财产类的实例,如
MyComponent.MySubProperty[i].Width := 666;
如果不问的话,那足以让你前进。
答案 1 :(得分:2)
您正在寻找的是嵌套对象。 Memo1
是TMemo
类的实例,该类具有Lines
属性,它是TStrings
类的实例,该类具有Add()
,{ {1}},Delete()
等属性和Insert()
等属性。
因此,在您的Count
中,它将需要component
,extract
和insert
的嵌套对象,并且这些对象将需要是具有以下类型的实例: modify
方法。