把一个结构体当做属性后碰到的问题


当我把一个"结构体"在类中当做属性后, 在实用中可以直接读取结构体成员, 但不能直接写入...

下面是由此引发的小练习:

unit Unit1;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;typeTForm1 = class(TForm)Button1: TButton;Button2: TButton;Button3: TButton;Button4: TButton;Button5: TButton;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button4Click(Sender: TObject);procedure Button3Click(Sender: TObject);procedure Button5Click(Sender: TObject);end;TMyClass = classstrict privateFPos: TPoint;procedure SetPos(const Value: TPoint);publicproperty Pos: TPoint read FPos write SetPos; //属性 Pos 对应一个点结构end;varForm1: TForm1;implementation{$R *.dfm}{ TMyClass }procedure TMyClass.SetPos(const Value: TPoint);
beginFPos := Value;
end;{测试}procedure TForm1.Button1Click(Sender: TObject);
varobj: TMyClass;
beginobj := TMyClass.Create;ShowMessageFmt('%d, %d', [obj.Pos.X, obj.Pos.Y]); //可以直接访问结构中的元素
//  obj.Pos.X := 11;  //但不能直接给结构中的元素赋值
//  obj.Pos.Y := 22;obj.Free;
end;//变通一
procedure TForm1.Button2Click(Sender: TObject);
varobj: TMyClass;
beginobj := TMyClass.Create;obj.Pos := Point(22,33); //ShowMessageFmt('%d, %d', [obj.Pos.X, obj.Pos.Y]);obj.Free;
end;//变通二
procedure TForm1.Button3Click(Sender: TObject);
varobj: TMyClass;pt: TPoint;
beginobj := TMyClass.Create;pt.X := 33;pt.Y := 44;obj.Pos := pt;ShowMessageFmt('%d, %d', [obj.Pos.X, obj.Pos.Y]);obj.Free;
end;//变通三(假如属性的 get 不是方法)
procedure TForm1.Button4Click(Sender: TObject);
varobj: TMyClass;p: PPoint;
beginobj := TMyClass.Create;p := Addr(obj.Pos);p.X := 44;p.Y := 55;ShowMessageFmt('%d, %d', [obj.Pos.X, obj.Pos.Y]);obj.Free;
end;//变通四(假如属性的 get 不是方法)
procedure TForm1.Button5Click(Sender: TObject);
varobj: TMyClass;
beginobj := TMyClass.Create;PPoint(Addr(obj.Pos)).X := 55;PPoint(Addr(obj.Pos)).Y := 66;ShowMessageFmt('%d, %d', [obj.Pos.X, obj.Pos.Y]);obj.Free;
end;end.


练习二:

unit Unit1;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs;typeTForm1 = class(TForm)procedure FormCreate(Sender: TObject);end;TMyClass = classprivateFPos: TPoint;function GetPos: TPoint;procedure SetPos(const Value: TPoint);function GetXY(const Index: Integer): Integer;procedure SetXY(const Index, Value: Integer);publicproperty Pos: TPoint read GetPos write SetPos;property X: Integer index 0 read GetXY write SetXY;property Y: Integer index 1 read GetXY write SetXY;end;varForm1: TForm1;implementation{$R *.dfm}{ TMyClass }function TMyClass.GetPos: TPoint;
beginResult := FPos;
end;procedure TMyClass.SetPos(const Value: TPoint);
beginFPos := Value;
end;function TMyClass.GetXY(const Index: Integer): Integer;
beginResult := 0;case Index of0: Result := FPos.X;1: Result := FPos.Y;end;
end;procedure TMyClass.SetXY(const Index, Value: Integer);
begincase Index of0: FPos.X := Value;1: FPos.Y := Value;end;
end;{测试}
procedure TForm1.FormCreate(Sender: TObject);
varobj: TMyClass;
beginobj := TMyClass.Create;obj.X := 11;obj.Y := 22;ShowMessageFmt('%d, %d', [obj.Pos.X, obj.Pos.Y]);obj.Free;
end;end.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/404537.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VC6 + OpenCV1.0实现图片缩放显示

用vc6新建一个win32控制台程序,代码: /*功能:实现加载jpg图片,并进行缩放显示开发环境: winXP vc6 openCV1.0头文件路径:D:\opensource\opencv1.0\cv\includeD:\opensource\opencv1.0\cxcore\includeD:\opensource\opencv1.0\ot…

view2.0移植自定义图标,带颜色修改

1.下载你的iconfont项目 1.将图标添加到项目,修改font-family值 2.下载项目打包文件,解压后如图所示 我们只关心里面的iconfont.css和iconfont.json 我们在这个文件夹,新建一个convert.js,内容如下 //convert.js let path1 "./iconf…

[react] react中你用过哪些第三方的中间件

[react] react中你用过哪些第三方的中间件 redux-thunk: Redux的异步处理方案,actionCreator中可以返回一个函数(即可以dispatch一个function),函数内在写异步的代码redux-saga: Redux的异步处理方案,没有破坏redux中dispatch一个…

从PeopleEditor控件中取出多用户并更新到列表

如果一个列表中有一个字段类型为用户或用户组,并且设置为用户,允许多值的话,那么用代码进行更新的时候就必须将这个字段的值赋成SPFieldUserValueCollection类型,以下代码即为从PeopleEditor控件中取出多个用户并返回一个SPFieldU…

[react] 怎样在react中使用innerHTML?

[react] 怎样在react中使用innerHTML? 使用dangerouslySetInnerHTML属性,该属性传入一个对象,对象中__html属性的值即时innerHTML的富文本代码 个人简介 我是歌谣,欢迎和大家一起交流前后端知识。放弃很容易, 但坚持一定很酷。…

vc++出现warningC4819的处理方法

From: http://blog.sina.com.cn/s/blog_93e339ba01014fiw.html 编译VC程序的时候出现如下提示警告: warning C4819: The file contains a character that cannot berepresented in the current code page (936). Save the file inUnicode format to prevent data l…

H3C——路由策略和策略路由实例配置

配置如下:[IP]int s0/2/0[IP-Serial0/2/0]ip add 202.112.1.10 28[IP-Serial0/2/0]int s0/2/1[IP-Serial0/2/1]ip add 61.67.1.10 28 [IP-Serial0/2/1]int lo0[IP-LoopBack0]ip add 10.10.10.10 32[IP]ip route-static 0.0.0.0 0 202.112.1.9 指条静态缺省路由到R1 …

[react] 请说说什么是useState?为什么要使用useState?

[react] 请说说什么是useState?为什么要使用useState? useState是React原生的Hook,它接受一个参数,这个参数可以是对象或者普通的基本数据类型的值,也可以是一个有返回值的函数,useState函数返回一个数组&…

vc2010+openCV1.0实现将指定目录下的所有jpg文件缩放后存放到目标文件夹

开发环境:winXP vc2010 OpenCV1.0 OpenCV1.0安装目录: D:\opensource\opencv1.0 源代码: /*功能:将指定目录下的所有JPG文件进行缩放后存放到目标文件夹开发环境: winXP vc2010 openCV1.0头文件路径:D:\opensource\opencv1.…

功能:人脉(People Hub)7-固定到“开始”屏幕

如果是您的亲人和密友,再或者是领导和重要客户。 您需要经常沟通,可以将他的联系人头像固定在开始屏幕上,方便您的沟通。方法:很简单,“长按该联系人”后,有菜单出现,选择弹出菜单中的“固定到‘…

[react] react中的setState缺点是什么呢?

[react] react中的setState缺点是什么呢? 调用时机不恰当的话可能引起循环调用的问题:比如在componentWillUpdate render componentDidUpdate调用都有可能引起这种问题setState可能会引用不必要的re-render:setState任何值都会引起组件的ren…

一个系统中同时使用VC6.0+OpenCV1.0和VS2010+OpenCV2.4.6.0的方法

From: http://blog.csdn.net/zzy7222872/article/details/6047446 以前用的是VC6.0OpenCV1.0的组合,一直用的很好。一般的图像处理算法都可以实现,现在突然想搞一下立体视觉方面的东西,查看了OpenCV的手册,发现立体视觉上的大部…

[react] 请说说什么是useEffect?

[react] 请说说什么是useEffect? useEffect是副作用函数,第一个参数是函数,第二个参数是依赖的数据数组,当依赖数组中的数据变化时,触发第一个参数函数的执行。有以下的几种使用方式 模拟componentDidMount&#xff…

《OEA - 实体扩展属性系统 - 设计方案说明书》

这篇设计文档是 12 月份写来参加公司的研发峰会的,自己倒是信心满满,不过最后还是没有入围。现在想想也没啥大用,所以贴出来,期待与园友交流。 文档有点长,没全部贴在博客中,有兴趣的可以下载附件中的 PDF。…

Python与C/C++ 模块相互调用

From: http://hi.baidu.com/jintuguo/item/45639b4e7cda3c9f833ae1bb Python调用C动态链接库 Python调用C库很简单,不经过任何封装打包成so,再使用python的ctypes调用即可。 <test.cpp 生成动态库的源文件> #include <stdio.h> extern "C" { void…

[react] 在构造函数中调用super(props)的目的是什么?

[react] 在构造函数中调用super(props)的目的是什么&#xff1f; 这是ES6的语法。class组件继承自React.Component&#xff0c;super(props)之后&#xff0c;有以下几个作用&#xff1a; 初始化props&#xff0c;虽然不进行super(props)操作&#xff0c;组件在实例化时react也…

coverage.py - python 单元测试覆盖率统计工具

前提&#xff1a;1.假定已经安装好coverage.py&#xff08;ubuntu 10.10python.2.7coverage3.5.1&#xff09;2.项目里有模块do.py以及测试它的单元测试模块doTEST.py命令行&#xff1a;$ cd /home/user1/workspace/hp1$ coverage run doTEST.py$ coverage report$ coverage ht…

[react] 为什么建议setState的第一个参数是callback而不是一个对象呢?

[react] 为什么建议setState的第一个参数是callback而不是一个对象呢&#xff1f; React 为了优化性能&#xff0c;有可能会将多个 setState() 调用合并为一次更新。 因为this.props和this.state 可能是异步更新的&#xff0c;你不能依赖他们的值计算下一个state(状态)。以下面…

Python Ctypes结构体指针处理(函数参数,函数返回)

参考网址: http://www.2cto.com/kf/201109/106444.html 本文演示了在python中调用C语言生成的动态库&#xff0c;返回结构体指针&#xff0c;并进行输出&#xff01; test.c(动态库源代码) // 编译生成动态库: gcc -g -fPIC -shared -o libtest.so test.c#include <stdio.h…

[react] react的状态提升是什么?使用场景有哪些

[react] react的状态提升是什么&#xff1f;使用场景有哪些 React的状态提升就是用户对子组件操作&#xff0c;子组件不改变自己的状态&#xff0c;通过自己的props把这个操作改变的数据传递给父组件&#xff0c;改变父组件的状态&#xff0c;从而改变受父组件控制的所有子组件…