- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已阅读官方文档,并且了解什么是类引用,但我无法理解何时以及为何与替代方案相比,它们是最佳解决方案。
文档中引用的示例是 TCollection,它可以使用 TCollectionItem 的任何后代进行实例化。使用类引用的理由是它允许您调用在编译时类型未知的类方法(我假设这是 TCollection 的编译时)。我只是不明白使用 TCollectionItemClass 作为参数如何优于使用 TCollectionItem。 TCollection 仍然能够保存 TCollectionItem 的任何后代,并且仍然能够调用 TCollectionItem 中声明的任何方法。不是吗?
将其与通用集合进行比较。 TObjectList 似乎提供与 TCollection 几乎相同的功能,并具有类型安全的附加优点。您无需从 TCollectionItem 继承来存储对象类型,并且可以根据需要将集合创建为特定类型。如果您需要从集合中访问项目的成员,您可以使用通用约束。除了 Delphi 2009 之前的程序员可以使用类引用这一事实之外,还有其他令人信服的理由在通用容器上使用它们吗?
文档中给出的另一个示例是将类引用传递给充当对象工厂的函数。在本例中,是一个用于创建 TControl 类型对象的工厂。它并不明显,但我假设 TControl 工厂正在调用传递给它的后代类型的构造函数,而不是 TControl 的构造函数。如果是这种情况,那么我至少开始看到使用类引用的一些原因。
所以我想我真正想了解的是类引用何时何地最合适,以及他们向开发人员购买什么?
最佳答案
元类都是关于“类过程”的。从基本的类
开始:
type
TAlgorithm = class
public
class procedure DoSomething;virtual;
end;
因为 DoSomething
是一个类过程
,我们可以在没有 TAlgorithm 实例的情况下调用它(它的行为与任何其他全局过程类似)。我们可以这样做:
TAlgorithm.DoSomething; // this is perfectly valid and doesn't require an instance of TAlgorithm
一旦我们完成了这个设置,我们就可以创建一些替代算法,所有算法都共享基本算法的一些部分。像这样:
type
TAlgorithm = class
protected
class procedure DoSomethingThatAllDescendentsNeedToDo;
public
class procedure DoSomething;virtual;
end;
TAlgorithmA = class(TAlgorithm)
public
class procedure DoSomething;override;
end;
TAlgorithmB = class(TAlgorithm)
public
class procedure DoSomething;override;
end;
我们现在有一个基类和两个后代类。以下代码完全有效,因为我们将这些方法声明为“类”方法:
TAlgorithm.DoSomething;
TAlgorithmA.DoSomething;
TAlgorithmB.DoSomething;
让我们介绍一下类
类型:
type
TAlgorithmClass = class of TAlgorithm;
procedure Test;
var X:TAlgorithmClass; // This holds a reference to the CLASS, not a instance of the CLASS!
begin
X := TAlgorithm; // Valid because TAlgorithmClass is supposed to be a "class of TAlgorithm"
X := TAlgorithmA; // Also valid because TAlgorithmA is an TAlgorithm!
X := TAlgorithmB;
end;
TAlgorithmClass 是一种可以像任何其他数据类型一样使用的数据类型,它可以存储在变量中,作为参数传递给函数。换句话说,我们可以这样做:
procedure CrunchSomeData(Algo:TAlgorithmClass);
begin
Algo.DoSomething;
end;
CrunchSomeData(TAlgorithmA);
在此示例中,过程 CrunchSomeData 可以使用算法的任何变体,只要它是 TAlgorithm 的后代即可。
以下是如何在现实应用程序中使用此行为的示例:想象一个薪资类型的应用程序,其中需要根据法律定义的算法计算一些数字。可以想象,这个算法会随着时间的推移而改变,因为法律有时会改变。我们的应用程序需要使用旧版本的算法计算当年的工资(使用最新的计算器)和其他年份的工资。事情可能是这样的:
// Algorithm definition
TTaxDeductionCalculator = class
public
class function ComputeTaxDeduction(Something, SomeOtherThing, ThisOtherThing):Currency;virtual;
end;
// Algorithm "factory"
function GetTaxDeductionCalculator(Year:Integer):TTaxDeductionCalculator;
begin
case Year of
2001: Result := TTaxDeductionCalculator_2001;
2006: Result := TTaxDeductionCalculator_2006;
else Result := TTaxDeductionCalculator_2010;
end;
end;
// And we'd use it from some other complex algorithm
procedure Compute;
begin
Taxes := (NetSalary - GetTaxDeductionCalculator(Year).ComputeTaxDeduction(...)) * 0.16;
end;
Delphi 构造函数的工作方式就像“类函数”;如果我们有一个元类,并且该元类知道虚拟构造函数,我们就能够创建后代类型的实例。当您点击“添加”按钮时,TCollection 的 IDE 编辑器将使用它来创建新项目。 TCollection 需要实现此功能的所有内容是 TCollectionItem 的元类。
关于Delphi 类引用...又名元类...何时使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3372981/
我正在构建一个游戏,例如 Same Game ,当我必须创建一个新关卡时,我刚刚运行了一个算法来用 N 种颜色填充板子,这个算法随机填充板子,但显然以这种方式生成的关卡并不是都有解决方案。 我必须做一
有两个 TFLearn 项目 TF Learn (aka Scikit Flow) https://github.com/tensorflow/tensorflow/tree/master/tenso
我正在尝试使用代码从 NSDictionary 中解析一个整数 [activeItem setData_id:[[NSString stringWithFormat:@"%@", [dict valu
是否有在线资源描述 VC10 与 VC9 相比代码生成的变化? 我不是在谈论 c++0x 和其他不错的功能(例如内置 static_assert),也不是在谈论精致的 UI。我需要知道的是,由于优化、
那里。我是 Swift 的初学者,正在尝试将旧程序转换为 Swift3。我设法修复了一堆错误,但我无法使该功能正常工作。 fileprivate func extractEntitlements(_
我正在尝试实现 Easy Game Center link但我得到这个错误: Cannot convert value of type 'NSRange' (aka 'NSRange') to exp
我是 Swift 的新手,我正在尝试按照 lynda.com 上的类(class)编写一个简单的类(class)此代码在视频中运行良好,但在我的系统上出现错误。 protocol JSONDecoda
我的代码中有一个错误,例如“无法将类型‘NSRange’(又名‘_NSRange’)的值转换为预期的参数类型‘Range’(又名‘Range’)”,但我不知道如何解决这个问题请任何人帮助我? 我在这里
检查字符串字符的范围时出现此错误... @objc func textField(textField: UITextField, shouldChangeCharactersInRange range
我正在尝试用属性字符串替换子字符串。以下是我的代码。 let searchText = self.searchBar.text! let name = item.firstName ?? "" let
string convert(string name) { string code = name[0]; ... } 我从这一行得到“没有从'value_type'(又名'char')到'st
标题说明了一切,我相信。我只是好奇 () -> () 是否充当函数的参数... class Test { var isAwesome = true func loadData (callbac
我正在编写这个很棒的应用程序,至少我认为它很棒,在 C 中与 GObject 的完美结合,过了一段时间我开始遇到这个非常非常奇怪的错误。我也相信已经注意到它并不总是出现。然而,这可能只是 IDE 的错
我一直在尝试使用类型族来抽象 UI 工具包。当我尝试使用 HLists (http://homepages.cwi.nl/~ralf/HList/) 来改进 API 时,我陷入了困境。 我的 API
在 Scala 中,as explained in the PR that introduced it , parasitic允许偷窃 execution time from other thread
我在网上看到过这个关于加载效果的回车示例,但我无法正确理解它。为什么它必须是 2 \rLoading 而不是 1?有人可以给我解释一下吗? for (int j = 0; j < 3; j++) {
我有一个字符串列表 strs = [ 'foo', 'bar' ] 和一些字典 foo = {'a': 1, 'b': 2}, bar = {'a': 3, 'b': 4}。我想使用 with_ite
我有一个由许多点组成的LineString(坐标字符串),我想计算沿线最近的距离(从第一个点到最后一个点,方向性很重要)到一个点,就像这样说的:"project()" calculation 如果我不
所以最近我确实为我的业务购买了一些模板。它们基于 HTML/CSS/JQUERY/JS/PHP。 问题如下:在我的领域,并发率非常高,人们下载模板并自己使用它们是很常见的。由于我确实为它们付出了很多,
我认为这很容易找到预制的,但似乎我在网上找到的任何解决方案都只能解决部分问题。 我想对用户提供的文件名列表进行排序(这些文件大多以人和/或地址命名),有时使用不同的语言(主要是德语,带有一些法语和意大
我是一名优秀的程序员,十分优秀!