- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我对面向对象编程有点陌生。 Delphi 具有属性,旨在成为比 getters/setters 更“优雅”的访问类数据的方式(在此处阅读 usage of property vs getters/setters in business classes )。
什么时候应该直接使用字段,什么时候应该在属性内使用getters/setters?我只在需要操作数据时进行猜测,但我不确定。
编辑:
省略一个只返回字段本身的值而不执行任何操作的 setter 是错误的吗?
property Field :integer read FField write FField;
最佳答案
对于初学者来说,来自 documentation on properties 的快速摘要:
A property, like a field, defines an attribute of an object. But while a field is merely a storage location whose contents can be examined and changed, a property associates specific actions with reading or modifying its data. Properties provide control over access to an object's attributes, and they allow attributes to be computed.
存储和访问的分离确实可以通过仅使用 getter 和 setter 来实现,而保留属性。这是事实,但您链接到的问题源于语言差异:Delphi 确实具有属性,并且那里的答案已经解释了为什么使用它们。两个最明显的原因是(1)更清晰的代码和(2)分配能力。我认为this answer已经相当广泛地处理它了。
此外,在不使用属性的情况下,总是需要 getter 和 setter,而使用属性则不需要。假设有 setter 实现,但没有 getter:属性可以直接读取字段。
当您只声明属性的名称及其类型时,Delphi 的类完成默认读取私有(private)字段和设置私有(private)字段的私有(private) setter。请注意,这只是默认配置,您可以再次根据需要进行修改。当您完全指定属性声明时,类完成将遵守并根据声明的需要添加私有(private)字段、getter 和/或 setter。
Is it wrong to omit a setter that does nothing but return the value of the field itself?
当一个属性没有 getter 或 setter 并且它只是读取和写入字段时,您可以得出这样的结论:除了一致之外没有任何区别。但事实并非如此。字段和属性具有不同的名称,因此可能具有不同的含义。意思是你可以给予。请参阅Using properties instead of fields in class methods of the same unit is a bad practice? .
... I'm gessing only when the data needs to be manipulated ...
嗯,这部分是正确的。操纵是众多原因之一。考虑 String
类型的 Price
属性及其私有(private)字段 FPrice
:
FPrice
是另一个字段的一部分时,或者当它超出此类的责任时,请注意,Price
属性非常初级。将其 setter 或 getter 留给将来的实现是很有可能的。但想象一下没有 setter 或 getter 就无法实现的更高级属性:
咨询前需要创建的字段:
function TMyObject.GetBarelyUsed: TRare;
begin
if FBarelyUsed = nil then
FBarelyUsed := TRare.Create(Self);
Result := FBarelyUsed;
end;
可以选择一个项目,但该项目本身不知道要做什么。相反,业主这样做了。请注意,在本例中完全没有私有(private)字段:
procedure TItem.SetSelected(Value: Boolean);
begin
if Value <> Selected then
begin
if Value then
Owner.Selection.Add(Self)
else
Owner.Selection.Remove(Self);
end;
end;
图像控件,专门用于查看您自己的图像格式。 FileName
属性的赋值涉及:检查正确的文件扩展名、检查文件是否存在、将文件名存储在私有(private)字段中、加载文件、调整图片的尺寸,或者撤消之前的赋值:
procedure TAwDxfImage.SetFileName(const Value: TFileName);
begin
if FFileName <> Value then
if SameText(ExtractFileExt(Value), '.' + SDxf) and
FileExists(Value) then
begin
FFileName := Value;
FGraphic.LoadFromFile(FFileName);
FGraphic.SetBounds(Width, Height);
end
else
begin
FFileName := '';
FGraphic.Clear;
end;
end;
<子> Source: NLDelphi
关于class - 德尔福类: Property vs Get/Set methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28884568/
我知道使用 GET 和 SET 函数的公共(public)变量的缺点/私有(private)变量的优点,但目前我正在使用 Ogre3D 开发自己的第一个“真实”游戏(C++)..同时,我有时需要 6-
我正在开发一个 GSM/GPRS 应用程序,它将每 10 秒报告一些值。我必须使用的 SIM 卡每月只有 15MB 可用数据。我使用的是 SIM900 GSM 芯片供您引用。 我到达服务器的方式是通过
这三者有什么区别:gets - 它获取带有 '\n' 的行gets.chomp - 它得到一行,但删除 '\n' 这样对吗? gets.chomp! 怎么样? 最佳答案 gets - 它得到一个末尾带
问题和我现在遇到的问题 脚本 顺便说一句,评论是挪威语的,如果它们看起来很奇怪哈哈 Connect-AzureAD #variabel $Users = Get-AzureADUser -All:$t
我现在面临的问题是获取一个 URL,如下所示: www.example.com/example.php?url=www.google.com 现在的问题是,如果我的网址中有一个 get,如下所示: w
我有一个 queryString 传递给 servlet 的 doGet() 方法,如下所示: count=9&preId0=-99&objId0=-99&preId1=-99&objId1=-99&
这是我在 Django 模板中的代码: {% for tag in tags %} {{ tag }} {% endfor %} 在view.py中: def tag_find(
我正在尝试在express.js中为我的网络应用程序创建一个路由系统,我需要知道是否需要使用app.get/post/put/delete.apply以编程方式设置多个功能对于一条路线。 也是如此 a
我正在通过示例查看 A.Mele Django,第 1 章 def post_list(request, category=None): object_list = Post.publishe
如果我想找到与IIS站点或应用程序关联的目录,我该怎么做? 我似乎无法从Get-Website和Get-WebApplication的对象的任何属性中找到任何允许我这样做的东西。 最佳答案 只需查看一
不知道发生了什么。当我执行以下代码时......它运行良好......但它产生了错误。如果我将以下内容粘贴到我的浏览器地址栏中并点击它,我会得到一个 URL。如果我通过 KRL http:get 输入
Curl 提供了一系列不同的带有 X 前缀的 http 方法调用,但也提供了不带 X 的相同方法。我两种都试过了,但我似乎无法弄清楚其中的区别。有人可以快速向我解释这两种操作有何不同吗? 最佳答案 默
request.GET.get 是什么意思?我在 Django 中看到类似的东西 page = request.GET.get('page', 1) 我认为它与类似的东西有关 « 它们是如
我正在从我的 Angular2 站点查询一些 Elasticsearch 服务器。为了帮助提高安全性,我们希望锁定对 GET 请求的访问权限。 Elasticsearch 支持带主体的 GET,但我在
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
调用 HTable.get(List) 返回的 Result 数组的顺序是什么? ? 我的意思是,假设与输入列表的顺序相同是否正确? 最佳答案 结果数组中的顺序将与输入列表的顺序相同。与批处理方法一样
所以我有一个看起来像这样的 JSON 数组: var myData = { foo : { biz : 'baz', fig : 'tree' } }
我正在学习 Ajax、javascript 和 html,并且有一个应用程序可以触发“get”请求,然后再触发另一个“get”请求。这些请求是用户按下按钮的结果。在我的 servlet 中,我使用 T
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
运行以下 cmdlet 适用于组成员(Amer 域中的组)中的所有用户,无论列出的用户位于哪个域: Get-ADGroupMember -Server amer 但是,当尝试通过管道传输到 Get-
我是一名优秀的程序员,十分优秀!