Delphi classes and objects (6)-encapsulation (member visibility)

Concept: Encapsulation refers to the use of classes to encapsulate data and methods through access control characters to achieve the hiding of functions.
Visibility of class members:
private		 // not visible. 
protected	 // Visible derived classes. 
public		 // visible. 
published	 // visible. Used for runtime information. 
automated	 // visible. Used for compatibility. 

// When out of range: {$ M +} (default) is published; {$ M-} is public

(1) Private member access character (private): indicates that the members of the class are private to the class, which is visible in the class, that is, the private members defined in the class can only be called by members or methods in the class.

(2) Protected member accessor (protected): The protected member can only be accessed by the class itself and subclasses derived from the class. If you only want subclasses to inherit some of the functions of the parent class, you can declare them as protected.

(3) Public member access character (public): No permission, as long as the member still exists, you can call the members of this class anywhere in the program, and the public members of the parent class can also be accessed in the derived subclass.

(4) Published member access character (published): The access rights are the same as the public access rights. The difference is that the published members can generate runtime type information at runtime, which is generally used for component attributes.

(5) Automated member access (automated): The access rights are the same as public access rights, the difference is that the published members will generate automated type information (ATI). It is only necessary to declare when defining or using automation objects (OLE technology).
If you do not specify a member access symbol in the class, the default access symbol for the members in the class is the public member access symbol. The

idea: If you abstract into different classes from computers and components, you can use these component classes to form a new one. The computer class, and a computer class provides only one boot for the user to operate at runtime, so that the initialization and startup process of other components
need not be provided to the user, but only needs to be provided to the computer class for execution. So in the computer class, you only need to make the method of running other components private or protected members.

// The two fields in this class are not encapsulated 
  TMyClass1 = class 
    FName: string ; 
    FAge: Integer; 
  end ; 

  // The two fields in this class are encapsulated and cannot be read or written outside 
  TMyClass2 = class 
    private 
      FName: string ; 
      FAge: Integer; 
    // public 
  end ; 

  // How to read and write? Use attributes 
  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 ;
   { 
    Now two fields in TMyClass3: FName, FAge and two methods: SetAge, SetName 
    are encapsulated in a private area (private) Internal, external reading and writing are not allowed, only for internal use. 

    However, the private encapsulation is invalid in this unit! 
    Now it is good, there is a strict identifier. 
  } 

  // This encapsulation is done, added before private strict; Now no one except yourself can access the private area. 
  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;

 

Guess you like

Origin www.cnblogs.com/fansizhe/p/12723808.html