Two Classes In Two Units With The Same Class Name in Delphi
I Have a case in Delphi such as:
Unit A contains class "One" Unit B contains class "One"
I'm in a class that uses unit A and want to use a static function from the class One in B; how do I do it? In C#, I'd write something like:
B.One.SomeProcedure
Or even I would use "using" to "rename" one names开发者_StackOverflowpace. What can I do in Delphi? (Removing the "uses" for unit A is not an option, nor is renaming one of the two classes.)
Edit: I'm using Delphi 2007.
What do you mean by "a class that uses"? Maybe you mean "a unit that uses"? If so, you need to add both A and B to the uses
clause. You can then distinguish between the two procedures by writing A.One.SomeProcedure
or B.One.SomeProcedure
. If you just write One.SomeProcedure
, the procedure in the unit listed last in the uses
clause will be used. [Here I assume that One
are classes containing class procedures SomeProcedure
. If SomeProcedure
is an ordinary procedure of the One
class, you need -- of course -- to create an object of this class and use this. You can then do myobj := A.One.Create
or myobj := B.One.Create
, where var myobj: A.One
or var myobj: B.One
, respectively.]
(Remember also that each unit contains two uses
clauses: one at the beginning of the interface
section and one at the beginning of the implementation
section. If you use something from unit A at line N, the uses
clause containing the unit A needs to be located on a line above N.)
Also notice that in Delphi, the class should be called TOne
, with the T
prefix. Everyone follows this convention, and it looks odd without it.
how about TNewClassA = class(UnitA.One) and TNewClassB = class(UnitB.One)?
You can use the following way: [UnitName].[Function]. For example B.SomeProcedure
You can do exactly the same thing. UnitName.ClassName.Method
, just like in C#.
精彩评论