What is the difference with these two sets of code

What is the difference between these two pieces of code

type IInterface1 = interface procedure Proc1; end; IInterface2 = interface procedure Proc2; end; TMyClass = class(TInterfacedObject, IInterface1, IInterface2) protected procedure Proc1; procedure Proc2; end; 

And the following:

 type IInterface1 = interface procedure Proc1; end; IInterface2 = interface(Interface1) procedure Proc2; end; TMyClass = class(TInterfacedObject, IInterface2) protected procedure Proc1; procedure Proc2; end; 

If they are one and the same, are there any advantages or problems with readability.

I assume the second means that you cannot write a class that implements IInterface2 without the implementation of IInterface1, while with the first you can.

+4
source share
4 answers

First, I assume the second example declaration for IInterface2 is a typo and should be

 IInterface2 = interface(Interface1) 

because inheriting from oneself is pointless (even if the compiler accepted it).

And "inheritance" is the key word to answer your question. In Example 1, the two interfaces are completely independent, and you can implement one, the other, or both without problems. In Example 2, you are right that you cannot implement interface2 without implementing interface1, but the reason is that this is because it makes interface1 part of interface2.

The difference, therefore, is primarily structural and organizational, and not just readability.

+4
source

Two pieces of code have very different effects and are almost not equivalent if we are talking about Delphi for Win32 (Delphi for .NET has different rules).

  • A class that implements its interface must implement all the members of this ancestor of the interface, but it does not implicitly implement the ancestors. Thus, attempts to assign instances of type TMyClass to locations of type IInterface1 will not be performed in the second case.
  • Due to the previous point, if IInterface1 and IInterface2 both had GUIDs, dynamic casts (using Supports or ' as ') of interface references with the target type IInterface1 would fail on TMyClass instances in the second case.
  • The IInterface2 interface has an additional method in the second case, which it is not in the first.
  • Values ​​of type IInterface2 in the second case are assigned to locations of type IInterface1; this is not true for the first case.

See in this example:

  type A_I1 = interface end; A_I2 = interface(A_I1) end; A_Class = class(TInterfacedObject, A_I2) end; procedure TestA; var a: A_Class; x: A_I1; begin a := A_Class.Create; x := a; // fails! end; type B_I1 = interface end; B_I2 = interface end; B_Class = class(TInterfacedObject, B_I1, B_I2) end; procedure TestB; var a: B_Class; x: B_I1; begin a := B_Class.Create; x := a; // succeeds! end; begin TestA; TestB; end. 
+6
source

Assuming you meant

 ... IInterface2 = interface(Interface1) ... 

I interpret it the same way you do, the second form requires that the class that implements Interface2 implement Interface1, but , while the first form does not work.

+1
source

I assume the second means that you cannot write a class that implements IInterface2 without the implementation of IInterface1, while with the first you can.

It will be a technical difference.

Which one best depends on what the interfaces actually are. Does the existence of IInterface2 make sense without it being also IInterface1?

If IInterface1 is “displayable” and IInterface2 is “persistent,” then probably the first option makes more sense. If IInterface1 is a "vehicle", and IInterface2 is a "truck", then the second option probably makes much more sense.

+1
source

Source: https://habr.com/ru/post/1277399/


All Articles