当前位置: 首页 > news >正文

夜夜做新郎网站关键词在线优化

夜夜做新郎网站,关键词在线优化,设计公司装修图,做ppt模板的网站有哪些14.3.2 特定类约束 ​ 如果您的泛型类需要使用某个特定子集的类&#xff08;特定层次结构&#xff09;&#xff0c;则可能需要根据给定基类指定约束。 ​ 例如&#xff0c;如果您声明&#xff1a; typeTCompClass<T: TComponent> class​ 则此泛型类的实例仅适用于组…
14.3.2 特定类约束

​ 如果您的泛型类需要使用某个特定子集的类(特定层次结构),则可能需要根据给定基类指定约束。

​ 例如,如果您声明:

typeTCompClass<T: TComponent> = class

​ 则此泛型类的实例仅适用于组件类,即任何TComponent后代类。这使您拥有一个非常特定的泛型类型(是的,听起来很奇怪,但这确实是它的实际情况),并且编译器将允许您在处理泛型类型时使用TComponent类的所有方法。

​ 如果这看起来非常强大,那么请三思。如果你考虑一下利用继承和类型兼容规则可以实现的功能,也许你可以使用传统的面向对象技术来解决同样的问题,而不必使用泛型类。我并不是说特定的类约束从来都没有用,但它肯定没有更高级别的类约束或(我觉得非常有趣的)基于接口的约束强大。

14.3.3 接口约束

​ 一般来说,更灵活的做法是只接受实现特定接口的类作为类型参数,而不是将一个泛型类约束为一个给定的类。这样就可以在泛型的实例上调用接口。在 C# 语言中,将接口约束用于泛型也很常见。让我先展示给你一个示例(来自IntfConstraint示例)。首先,我们需要声明一个接口:

typeIGetValue = interface['{60700EC4-2CDA-4CD1-A1A2-07973D9D2444}']function GetValue: Integer;procedure SetValue(Value: Integer);property Value: Integer read GetValue write SetValue;end;

​ 接下来,我们可以定义一个实现它的类:

typeTGetValue = class(TNoRefCountObject, IGetValue)privateFValue: Integer;publicconstructor Create(Value: Integer = 0);function GetValue: Integer;procedure SetValue(Value: Integer);end;

​ 在限制为实现特定接口的类型的泛型类的定义中,事情开始变得有趣:

typeTInftClass<T: IGetValue> = classprivateFVal1, FVal2: T; // Or IGetValuepublicprocedure Set1(Val: T);procedure Set2(Val: T);function GetMin: Integer;function GetAverage: Integer;procedure IncreaseByTen;end;

​ 请注意,在这个类的泛型方法的代码中,我们可以编写:

function TInftClass<T>.GetMin: Integer;
beginResult := Min(FVal1.GetValue, FVal2.GetValue);
end;procedure TInftClass<T>.IncreaseByTen;
beginFVal1.SetValue(FVal1.GetValue + 10);FVal2.Value := FVal2.Value + 10;
end;

​ 有了所有这些定义,我们现在可以按以下方式使用泛型类:

procedure TFormIntfConstraint.BtnValueClick(Sender: TObject);
varIClass: TInftClass<TGetValue>;
beginIClass := TInftClass<TGetValue>.Create;tryIClass.Set1(TGetValue.Create(5));IClass.Set2(TGetValue.Create(25));Show('Average: ' + IntToStr(IClass.GetAverage));IClass.IncreaseByTen;Show('Min: ' + IntToStr(IClass.GetMin));finallyIClass.FVal1.Free;IClass.FVal2.Free;IClass.Free;end;
end;

​ 为了展示这个泛型类的灵活性,我为接口创建了另一个完全不同的实现方法:

type
TButtonValue = class(TButton, IGetValue)
publicfunction GetValue: Integer;procedure SetValue(Value: Integer);class function MakeTButtonValue(Owner: TComponent; Parent: TWinControl): TButtonValue;
end;function TButtonValue.GetValue: Integer;
beginResult := Left; // use base class property
end;procedure TButtonValue.SetValue(Value: Integer);
beginLeft := Value; // use base class property
end;

​ 该类函数(此处未显示)在父控件中创建了一个随机位置的按钮。位置创建一个按钮,并在以下示例代码中使用:

procedure TFormIntfConstraint.BtnValueButtonClick(Sender: TObject);
varIClass: TInftClass<TButtonValue>;
beginIClass := TInftClass<TButtonValue>.Create;tryIClass.Set1(TButtonValue.MakeTButtonValue(Self, ScrollBox1));IClass.Set2(TButtonValue.MakeTButtonValue(Self, ScrollBox1));Show('Average: ' + IntToStr(IClass.GetAverage));Show('Min: ' + IntToStr(IClass.GetMin));IClass.IncreaseByTen;Show('New Average: ' + IntToStr(IClass.GetAverage));finallyIClass.Free;end;
end;
http://www.shuangfujiaoyu.com/news/52026.html

相关文章:

  • 做木工的网站关键词优化简易
  • 怎样做网站seo百度关键词多少钱一个月
  • 做酒店网站所用到的算法临沂seo顾问
  • asp net网站开发语言的特点关键词优化如何做
  • 合肥网站制作公司网络营销师课程
  • mvc网站开发实例教程太原百度快照优化排名
  • 手机微网站开发教程西地那非片吃了能延时多久
  • 阿里网站年费续费怎么做分录网络黄页推广软件哪个好用
  • 网站建设与运营的课程标准百度一下你就知道百度首页
  • 建站之星官网建设广告推广方式有哪几种
  • 建行购物网站深圳网络营销推广招聘网
  • 网站后端开发流程十大营销策略
  • wordpress issetseo怎么做优化排名
  • 无锡网络营销推广软件seo搜索引擎
  • 云南哪几个建网站公司温州高端网站建设
  • 哪些网站做夜场女孩多常用的关键词优化策略有哪些
  • 做h的游戏视频网站泉州百度seo
  • b2b网站开发公司如何在百度打广告
  • 导航网站备案指数型基金
  • 成华区微信网站建设公司百度云盘官网
  • 网站开发对数据库的要求指数查询
  • 做网站软件frontpage网站推广优化方案
  • dedecms做网站网站站点查询
  • 网站建设的行业客户网络营销主要做什么
  • 网络销售工作怎么样佛山seo整站优化
  • 西安品牌网站建设服务商优化方案官方网站
  • 做网站公司 汉狮网络班级优化大师怎么加入班级
  • php网站微信登录怎么做seo搜索引擎排名优化
  • jsp购物网站开发环境无排名优化
  • 空间站免费版下载百度关键词优化是什么意思