Why use property in a class?

后端 未结 7 458
闹比i
闹比i 2020-12-17 00:34

I was just wondering about why should I use property in a class instead of \"normal\" variables (class attributes?). What I mean is this:

TSampleClass = clas         


        
7条回答
  •  一个人的身影
    2020-12-17 00:59

    You cant monitor the change in a variable without a property.

    your read/writes for property dont have to be a variable they can be functions. And then you can manage the "onChange" of a property.

    eg

    TmyChange = procedure(Sender: Tobject) of object;
    
    
    private 
    Fchange : TmyChange;
    
    public
    property SomeInfo: integer read getFoo write setFoo;
    property onChange : TmyChange read Fchange write Fchange;
    
    function getFoo : integer
    begin
      return localFoo;
    end;
    
    function setFoo (value : integer)
    begin
      // validate incoming value
       localFoo=value;
      if assigned(Fchange) then Fchange(self);
    end;
    

提交回复
热议问题