Ir al contenido


Foto

[RESUELTO] private, strict private, protected, strict protected e interface


  • Por favor identifícate para responder
2 respuestas en este tema

#1 Rolphy Reyes

Rolphy Reyes

    Advanced Member

  • Moderadores
  • PipPipPip
  • 2.092 mensajes
  • LocationRepública Dominicana

Escrito 24 marzo 2011 - 10:13

Saludos.

Me he encontrado con lo que creo que es un Bug en Delphi 2007,  digo creo porque no estoy del todo seguro y espero ustedes me puedan ayudar a entender el porque.

Estoy creando un par de plantillas para mis desarrollos personales, y me dije: "Ya que estoy haciendo esto nuevo, ¿Porque no usar las nuevas características de mi Delphi?" .

Observen este código:


delphi
  1. unit Unit6;
  2.  
  3. interface
  4.  
  5. type
  6.   THola = class sealed(TObject)
  7.   strict private
  8.     FHola : Integer;
  9.   Strict protected
  10.     Function GetHola : Integer;
  11.     property Hola : Integer read GetHola;
  12.   public
  13.  
  14.     constructor Create(const AHOla : Integer);
  15.   end;
  16.  
  17. implementation
  18.  
  19. { THola }
  20.  
  21. constructor THola.Create(const AHOla: Integer);
  22. begin
  23.   FHola := AHOla;
  24. end;
  25.  
  26. function THola.GetHola: Integer;
  27. begin
  28.   Result := FHola;
  29. end;
  30.  
  31. end.



Hasta aquí todo bien, compila maravillosamente, aprovecho para recordar la definición de las palabras reservadas que he utilizado:

strict private: The private keyword actually creates a " friendship" relationship between classes in the same unit.  The strict private declaration creates a true private field, not viewable by any other class, not even classes in the same unit.
strict protected: Similar to the strict private declaration, strict protected creates a true protected member, visible only to the declaring class and its descendents.
class sealed: Classes marked as sealed cannot be inherited from.


delphi
  1. type
  2.   TAbstractClass = class sealed
  3.     procedure SomeProcedure;
  4. end;


Para tener mayor flexibilidad (desde mi punto de vista), decidí utilizar interfaces y realice lo siguiente:


delphi
  1. unit Unit7;
  2.  
  3. interface
  4.  
  5. type
  6.  
  7.   IDefault = interface(IInterface)
  8.   ['{66CA72EE-98FD-4DA5-9C07-59805D65E5F7}']
  9.     function GetNewProperty: Integer;
  10.     property NewProperty: Integer read GetNewProperty;
  11.  
  12.   end;
  13.  
  14. implementation
  15.  
  16. end.



Ahora realizo la implementación de la interface:


delphi
  1. unit Unit8;
  2.  
  3. interface
  4.  
  5. uses
  6.   Unit7;
  7.  
  8. type
  9.   TDefault = class sealed(TInterfacedObject, IDefault)
  10.   strict private
  11.     FNewProperty : Integer;
  12.  
  13.   strict protected
  14.     function GetNewProperty: Integer;
  15.     property NewProperty: Integer read GetNewProperty;
  16.   public
  17.     constructor Create(const ANewProperty : Integer);
  18.   end;
  19.  
  20. implementation
  21.  
  22. { TDefault }
  23.  
  24. constructor TDefault.Create(const ANewProperty: Integer);
  25. begin
  26.   FNewProperty := ANewProperty;
  27. end;
  28.  
  29. function TDefault.GetNewProperty : Integer;
  30. begin
  31.   Result := FNewProperty;
  32. end;
  33.  
  34. end.



Pues, ¿Qué creen?.  Mi querido Delphi me da error diciéndome: [DCC Error] Unit8.pas(22): E2003 Undeclared identifier: 'GetNewProperty' .  Error del que no entiendo la lógica.

Si movemos el método y la propiedad a strict private, salta el mismo error; ahora bien si, ponemos nuevamente dichos métodos a strict protected y eliminamos la sentencia strict y dejamos protected, entonces si compila.

Se sobre entiende que al utilizar interfaces todos los métodos son públicos sin importar la visibilidad de donde se implemento, si se accede desde una variable tipo interface.
El siguiente código lo hago con la intención de mostrar lo que acabo de decir, no necesariamente deba de compilar:


delphi
  1. type
  2.  
  3.   IDefault = interface(IInterface)
  4.   ['{66CA72EE-98FD-4DA5-9C07-59805D65E5F7}']
  5.     function GetNewProperty: Integer;
  6.     property NewProperty: Integer read GetNewProperty;
  7.  
  8.   end;
  9.  
  10.  
  11.   TDefault = class (TInterfacedObject, IDefault)
  12.   private
  13.     FNewProperty : Integer;
  14.   protected
  15.     function GetNewProperty: Integer;
  16.     property NewProperty: Integer read GetNewProperty;
  17.   public
  18.     constructor Create(const ANewProperty : Integer);
  19.   end;
  20.  
  21. TForm1.Button1Click(Sender : TObject);
  22. var
  23.   X : IDefault;
  24. begin
  25.   X := TDefault.Create(1);
  26.   ShowMessage(IntToStr(X.GetNewProperty));
  27.   ShowMessage(IntToStr(X.NewProperty));
  28. end;



Como ven, el código anterior accede a los métodos sin importar la visibilidad donde fueron implementados.

Espero haber sido lo bastante claro, ojala y puedan echarme una mano para tratar de entender el porque de ese error.

  • 0

#2 Rolphy Reyes

Rolphy Reyes

    Advanced Member

  • Moderadores
  • PipPipPip
  • 2.092 mensajes
  • LocationRepública Dominicana

Escrito 25 marzo 2011 - 07:02

Saludos.

Acabo de confirmarlo, es realmente un Bug de Delphi 2007, realice las pruebas en el trabajo que en una PC tenemos instalado Delphi XE Update 1.

Ahora me siento más encabretao con esa gente (CodeGear, Embarcadero), publican 3 Updates para el D2007 y no pudieron corregir ese error; pudieron corregir ese error para XE y quien sabe si ya lo hicieron para las demás versiones (2009, 2010) pero no pudieron liberar un Update o BugFix para el D2007.
  • 0

#3 egostar

egostar

    missing my father, I love my mother.

  • Administrador
  • 14.469 mensajes
  • LocationMéxico

Escrito 26 marzo 2011 - 11:32

Vaya, pues si que encabrita que no se corrigiera el bug, sobre todo cuando el bug te detiene para continuar tus actividades.

Salud OS
  • 0




IP.Board spam blocked by CleanTalk.