码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi 封装(3)-属性的封装

时间:2018-10-28 21:55:44      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:button   false   save   sig   yellow   char   form   info   create   

技术分享图片

type 
TMan = class(TObject)
public
    FAge: integer;
end;

技术分享图片

type 
TMan = class(TObject)
private
    FAge: Integer;
    function GetAge: integer;
    procedure SetAge(Value: integer);
public
    property Age: integer read GetAge write SetAge;
end;

技术分享图片

procedure TMan.SetAge(Value: integer);
begin
    if(Value > 0) and (Value < 120) then
        FAge := Value;
end;

技术分享图片

procedure TMan.SetPhoto(value: TPicture);
begin
    if FPhoto.Picture = value then
        exit;
    Repaint;
end;

技术分享图片

 

 

unit Unit1;

uses   
    //...

type   
TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
private
    FForm:TForm;
public
    property Form: TType read FForm write FForm;
end;

var
    Form1: TForm1;



implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form2 := TFrom2.create(self);
end;

procedure TFrom1.Button1Click(Sender: TObject);
begin
    if Assigned(FForm) then
        TForm2(FForm).Shoe;//访问的是内部成员FForm,主要FForm需要转型.
end;

procedure TFrom1.Button2Click(Sender: TObject);
begin
   self.caption := Hi;//在TForm1类中实用self代替Form1.
end;

//一下是项目文件中的内容
program Project1;
uses
    Forms,
    unit1 in Unit1.pas{Form1},
    Unit2 in Unit2.pas{Form2};

begin
    Application.Initialize;
    Application.CreateForm(TForm1,Form1);
    Application.CreateForm(TForm2,Form2);
    //通过属性传递Form2的引用
    Form1.FForm := Form2;
    Application.Run;
end;

type 
TMan = class(TObject)
private
    FAge: Integer;
    function GetAge: integer;
    procedure SetAge(Value: integer);
public
    property Age: integer read GetAge write SetAge;
end;

procedure TMan.SetAge(Value: integer);
begin
    if(Value > 0) and (Value < 120) then
        FAge := Value;
end;

procedure TMan.SetPhoto(value: TPicture);
begin
    if FPhoto.Picture = value then
        exit;
    Repaint;
end;

unit uCreateManClass;

interface

uses    
    Dialogs;

type    
TSkinColor = (scWhite, scYellow, scBlack, scDark);
TMan = class(TObject)
private
    FAge: integer;
    FLanguage: string;
    FMale: boolean;
    FName: string;
    FSkinColor: TSkinColor;
    FValidData: boolean;
    function GetAge: integer;
    function GetColor: TSkinColor;
    function GetLanguage: string;
    function GetMale: boolean;
    function GetName: string;
    function SetAge(value: integer)
    function SetColor(value: TSkinColor);
    procedure SetLanguage(valude: string;);
    procedure SetMale(valude: boolean);
    procedure SetName(value: string);
public
    function retrieve:Boolean;
    function save:boolean;
    procedure SayHello(Words: pchar);
    property Age: TType read FAge write FAge;
    property Language: TType read FLanguage write FLanguage;
    property Male: TType read FMale write FMale;
    property Name: TType read FName write FName;
    property SkinColor: TType read FSkinColor write FSkinColor;
    property ValidData: TType read FValidData write FValidData;
end;

implementation

function TMan.GetAge: integer;
begin   
    result := FAge;
end;

function TMan.GetColor: TSkinColor;
begin
    result := FSkinColor;
end;

function TMan.GetLanguage: string;
begin
    result := FLanguage;
end;

function TMan.GetMale: boolean;
begin
    result := FMale;
end;

function TMan.retrieve: boolean;
begin
end;

function TMan.save: boolean;
begin
end;

procedure TMan.SayHello(words: pchar);
begin
    ShowMessage(words);
end;

procedure TMan.SetAge(value: integer)
begin
    if(value > 0) and (value < 120) then
        FAge := value
    else
        FValidData := False;
end;

procedure TMan.SetColor(Value: TSkinColor);
begin
    if(value <> FSkinColor) then
        FSkinColor := value;
end;

procedure TMan.SetLanguage(value: string)
begin
    if(value <> FLanguage)then
        FLanguage := value;
end;

procedure TMan.SetMale(value: boolean);
begin
    if(value <> FMale) then
        FMale := value;
end;

procedure TMan.SetName(value: string);
begin
    if(value <> FName) then
        FName := valude;
end;

技术分享图片

 

Delphi 封装(3)-属性的封装

标签:button   false   save   sig   yellow   char   form   info   create   

原文地址:https://www.cnblogs.com/YiShen/p/9867142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!