Are units in delphi same as classes in other languages? [closed]

旧街凉风 提交于 2019-12-08 14:33:26

No. A unit is a source code file in Delphi. You can essentially think of it as a namespace whose scope is exactly the same as the current file.

Within a unit, you can define classes, with type definition syntax. It looks like this:

type
  TMyClass = class(TParentClass)
  private
    //private members go here
  protected
    //protected members go here
  public
    //public members go here
  end;

Any methods are declared below the type declaration, not inline, which makes the code easier to read because you can see the composition of a class at a glance instead of having to wade through its implementation.

Furthermore, each unit has two main sections, called interface and implementation. A type declaration can be placed in either section, but implementing code is not valid in interface. This allows for a language concept similar to Java's or C#'s public and private classes: any type declared in interface is visible to other units that use this unit ("public"), while any type declared in implementation is visible only within the same unit.

To add to Mason's answer - a general Unit structure looks something like this :

Unit UnitName;    
  interface
    //forward declaration of classes, methods, and variables)
    uses 
      //list of imported dependencies needed to satisfy interface declarations
      Windows, Messages, Classes;
    const 
      // global constants
      THE_NUMBER_THREE = 3;
    type // declaration and definition of classes, type aliases, etc 
      IDoSomething = Interface(IInterface)
        function GetIsFoo : Boolean;
        property isFoo : Boolean read GetIsFoo;
      end;
      TMyArray = Array [1..5] of double;
      TMyClass = Class(TObject)
        //class definition
        procedure DoThis(args : someType);
      end;
      TAnotherClass = Class(TSomethingElse)
        //class definition
      end;        
    //(global methods)
    function DoSomething(arg : Type) : returnType;        
    var  //global variables
      someGlobal : boolean;      
  implementation
    uses
      //list of imported dependencies needed to satisfy implementation
    const
      //global constants with unit scope (visible to units importing this one)
    type
      //same as above, only visible within this or importing units
    var
      //global variables with unit scope (visible to units importing this one) 
    procedure UnitProc(args:someType)
    begin
      //global method with unit scope, visible within this or importing units
      //note no forward declaration!
    end;       
    procedure TMyClass.DoThis(args : someType)
    begin
      //implement interface declarations
    end;
    function DoSomething(arg : Type) : returnType;    
    begin
      // do something
    end;
  initialization
    //global code - runs at application start 
  finalization
    //global code - runs at application end
end. // end of unit

Obviously, every unit does not need all of these sections, but I think these are all of the possible sections that can be included. It took me a while to figure all of this out when I first lept into Delphi and I probably would have done well with a map like this so I provide it in case it is helpful.

Inside Unit of Delphi, or library of C++ Builder you can build more than one class at the same time. The IDEs for JAVA frequently uses one class to one file, it differs from delphi or C++ Builder but you can do this pratice to Delphi or C++ Builder too.

The classes in each language has your particularities. Is possible to think in POO for all in the same way but to implement it differs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!