理解Delphi的类(六)

//这个类中的两个字段没有封装

TMyClass1 = class
  FName: string;
  FAge: Integer;
 end;

//这个类中的两个字段封装了, 外部不能读写

TMyClass2 = class
  private
   FName: string;
   FAge: Integer;
  //public
 end;

//那怎么读写? 用属性啊

TMyClass3 = class
 private
  FName: String;
  FAge: Integer;
  procedure SetAge(const Value: Integer);
  procedure SetName(const Value: String);
 published
  property Name: String read FName write SetName;
  property Age: Integer read FAge write SetAge;
 end;
 {
  现在 TMyClass3 中的两个字段: FName、FAge 和两个方法: SetAge、SetName
  都被封装在私有区(private)内, 不允许外部读写, 只能是内部使用.
  不过, private 的封装在本单元内是无效的!
  现在好了, 有了 strict 标识符.
 }

//这个封装好了, 在 private 前加了 strict ; 现在除了自己谁也访问不了私有 区.

TMyClass4 = class
 strict private
  FName: String;
  FAge: Integer;
  procedure SetAge(const Value: Integer);
  procedure SetName(const Value: String);
 published
  property Name: String read FName write SetName;
  property Age: Integer read FAge write SetAge;
 end;

{封装的目的就是隐藏实现细节、保证数据安全}

时间: 2024-11-18 05:22:35

理解Delphi的类(六)的相关文章

理解Delphi的类(十二)

关于属性的话题还有很多, 譬如: 数组属性.默认属性.class 属性等等. 先总结一下前面提到过的属性吧. TMyClass = class(TObject) private FName: string; procedure SetName(const Value: string); published property Name: string read FName write SetName; end; 1.属性用 property 定义; 2.read.write 两个关键字至少要存在一

理解Delphi的类(十一)

[1] - 虚方法与动态方法 方法来到类中, 以前的特点基本都在; 因为类一般是存在于一个继承链中, 所以就有了一些新的概念, 譬如: 继承.覆盖;也 有了很多新名称, 譬如: 静态方法.虚方法.动态方法.抽象方法.类方法.消息方法. 先从虚方法与动态方法开始吧 //下面的类中就定义了两个虚方法(virtual).两个动态方法(dynamic) TMyClass = class procedure Proc1(x,y: Real); virtual; function Fun1(x,y: Rea

理解Delphi的类(七)

什么是多态? 我的理解就是: 同样一个方法, 在不同的对象里会有不同的实现, 仅此 而已. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; pr

理解Delphi的类(十)

类中包含字段.方法和属性(属性包含事件); 字段是靠方法组织与操作的; 属性也只 是方便和规范了字段与方法的使用. 因此我觉得: 方法是最重要的. 方法无处不在, 它不仅仅存在与类中. 先从非类中的方法谈起吧, 因为类中的方法也 拥有全部这些特性. 离开类的怀抱, 我们更喜欢把方法叫做过程或函数. //要点1: 过程用 procedure 定义, 函数用 function 定义; 过程没有返回值, 函数 需要返回值. procedure MyProc; begin ShowMessage('ok

理解Delphi的类(八)

//标准语法 TMyClass1 = class(TObject) end; //如果是继承自 TObject 可以省略 TMyClass2 = class end; //可以实现多个接口; 实现接口时经常用到 TInterfacedObject 类, 它实现了接口的默 认方法 TMyClass3 = class(TInterfacedObject, Interface1, Interface2) end; //现在 TMyClass4 相当于 TObject 的别名 TMyClass4 = c

理解Delphi的类(五)

先新建一个 VCL Forms Application 工程, 代码中就已经出现了两个类: 一个是 TForm 类; 一个是 TForm1 类; TForm1 继承于 TForm. TForm 是 TForm1 的父类; TForm1 是 TForm 的子类. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls

理解Delphi的类(四)

先勾画一下思路: 1.建立一个类, 里面有年龄字段 FAge; 2.通过 Age 属性读写 FAge; 3.如果输入的年龄刚好是 100 岁, 将会激发一个事件, 这个事件我们给它命名为: OnHundred unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TFor

理解Delphi的类(三)

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end;//TMyClass1 类里面只有两个字段(变量来到类里面称做字段) TMyC

理解Delphi的类(二)

说到"类", 就会提到: 属性.方法.事件 (这是类包含的内容); 封装.继承.多态 (这是类的主要用途). 下面定义并调用了了一个过程 MyProc.一个函数 MyFun. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: