问题
I need to write some Delphi code, but I have no prior experience with Delphi. I've seen people writing some code, known as unit1
or unit2
and import it using the code inside them. So, can I see the unit as a class in Java or C#?
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/17117871/are-units-in-delphi-same-as-classes-in-other-languages