delphi中的单元是否与其他语言中的类相同?

delphi中的单元是否与其他语言中的类相同?,delphi,class,delphi-units,Delphi,Class,Delphi Units,我需要写一些Delphi代码,但我以前没有使用Delphi的经验。我见过有人编写一些代码,称为unit1或unit2,并使用其中的代码导入。那么,我可以在Java或C中将单元视为类吗?否。单元是Delphi中的源代码文件。实际上,您可以将其视为一个命名空间,其作用域与当前文件完全相同 在一个单元中,可以使用类型定义语法定义类。看起来是这样的: type TMyClass = class(TParentClass) private //private members go here

我需要写一些Delphi代码,但我以前没有使用Delphi的经验。我见过有人编写一些代码,称为unit1或unit2,并使用其中的代码导入。那么,我可以在Java或C中将单元视为类吗?

否。单元是Delphi中的源代码文件。实际上,您可以将其视为一个命名空间,其作用域与当前文件完全相同

在一个单元中,可以使用类型定义语法定义类。看起来是这样的:

type
  TMyClass = class(TParentClass)
  private
    //private members go here
  protected
    //protected members go here
  public
    //public members go here
  end;
任何方法都是在类型声明下面声明的,而不是内联的,这使得代码更易于阅读,因为您可以一眼看到类的组成,而不必费力地完成它的实现

此外,每个单元有两个主要部分,称为接口和实现。类型声明可以放在任一节中,但实现代码在接口中无效。这允许一种类似于Java或C的公共和私有类的语言概念:接口中声明的任何类型对使用该单元的其他单元都可见,而实现中声明的任何类型仅在同一单元中可见。

否。单元是Delphi中的源代码文件。实际上,您可以将其视为一个命名空间,其作用域与当前文件完全相同

在一个单元中,可以使用类型定义语法定义类。看起来是这样的:

type
  TMyClass = class(TParentClass)
  private
    //private members go here
  protected
    //protected members go here
  public
    //public members go here
  end;
任何方法都是在类型声明下面声明的,而不是内联的,这使得代码更易于阅读,因为您可以一眼看到类的组成,而不必费力地完成它的实现


此外,每个单元有两个主要部分,称为接口和实现。类型声明可以放在任一节中,但实现代码在接口中无效。这允许一种类似于Java或C的公共和私有类的语言概念:接口中声明的任何类型对使用该单元的其他单元都可见,而实现中声明的任何类型仅在同一单元中可见。

要补充Mason的答案,一般单元结构如下所示:

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

显然,每个单元并不需要所有这些部分,但我认为这些都是可能包含的部分。当我第一次进入Delphi时,我花了一段时间才弄清楚这一切,我可能会很好地使用这样的地图,所以我提供了它,以防它有帮助。

为了补充Mason的答案,一般的单元结构如下所示:

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

显然,每个单元并不需要所有这些部分,但我认为这些都是可能包含的部分。当我第一次进入Delphi时,我花了一段时间来把所有这些都弄清楚,我可能会像这样做得很好,所以我提供它以防有帮助。

< P> > Delphi内部单元,或者C++ Builder库,可以同时构建多个类。java的IDE经常使用一个类到一个文件,它不同于Delphi或C++ Builder,但是你也可以对Delphi或C++ Builder进行此练习。
每种语言的课程都有自己的特点。可以用同样的方式在POO中进行思考,但是实现它的不同。Delphi的内部单元

< P>或C++ Builder库可以同时构建多个类。java的IDE经常使用一个类到一个文件,它不同于Delphi或C++ Builder,但是你也可以对Delphi或C++ Builder进行此练习。
每种语言的课程都有自己的特点。在《公安条例》中,所有人的思维方式都是一样的,但实现方式却不同。

这不是学习一门新语言的方法。找一本好的参考书或书,花点时间学习。你需要从介绍Pascal/Delphi开始。有一本免费的书——看看印刷书籍广告下面,或者买一本便宜的书。通过Google和Bing搜索,或者在文档中提供Delphi教程。这不是学习新语言的方法。找一本好的参考书或书,花点时间学习。你需要从介绍Pascal/Delphi开始。有一本免费的书——看看印刷书籍广告下面,或者买一本便宜的书。通过Google和Bing搜索或文档本身中的提供了Delphi教程。