3

I am new on Delphi, i have some code writed with Java. It has an inner class structure. But i dont know how to convert to Delphi.

unit resolutionSet;

interface

uses
  Winapi.Windows, System.Math;

type
TResolutionSet = class
  type
  TResolutionLevel = class
  private
    { private declarations }
    discardLayers : Integer;
    dims          : TRect;
    function getZoomLevel : Integer;
    function getZoomPercent : Double;
    function getResolutionBounds : TRect;
    constructor Create(_discardLayers : Integer; _dims : TRect) overload;
  protected
    { protected declarations }
  public
    { public declarations }
  published
    { published declarations }
  end;
private
  { private declarations }
  resolutions : array of TResolutionLevel;
protected
  { protected declarations }
public
  { public declarations }
  function getResolutionLevel(_currRes : TResolutionLevel; _delta : Integer) : TResolutionLevel overload;
  function getResolutionLevel(_index : Integer) : TResolutionLevel overload;
  function getClosestResolutionLevel(_source : Double; _target : Double) : TResolutionLevel;
  procedure addResolutionLevel(_discardLayer : Integer; _dims : TRect);
  constructor Create(_numResolutions : Integer) overload;
published
  { published declarations }
end;
implementation

constructor TResolutionSet.Create(_numResolutions : Integer) overload;
begin
  SetLength(resolutions, _numResolutions);
end;

procedure TResolutionSet.addResolutionLevel(_discardLayer : Integer; _dims : TRect);
begin
  resolutions[_discardLayer]:= TResolutionLevel.Create(_discardLayer, _dims);
end;

function TResolutionSet.getResolutionLevel(_currRes : TResolutionLevel; _delta : Integer) : TResolutionLevel overload;
begin
  //Result:= resolutions
end;

function TResolutionSet.getResolutionLevel(_index : Integer) : TResolutionLevel overload;
begin
  Result:= resolutions[_index];
end;

function TResolutionSet.getClosestResolutionLevel(_source : Double; _target : Double) : TResolutionLevel;
var
  idx : Integer = 0;
  i   : Integer;
begin
  for i := Length(resolutions)-1 downto 0 do
  begin
    idx:= i;
    if (_source * Power(2, resolutions[i].getZoomLevel())) <= _target then
      break;
  end;
  Result:= resolutions[idx];
end;

end.

I declared TResolutionLevel for inner class, but how can i implement this class' s method in where?

4
  • This syntax is not directly analogous to Java inner classes Commented Jan 8, 2015 at 13:28
  • 1
    Since you are new to Delphi may I commend the documentation to you: docwiki.embarcadero.com/RADStudio/en/Nested_Type_Declarations Commented Jan 8, 2015 at 13:29
  • thank you buddy, ill check there Commented Jan 8, 2015 at 13:33
  • 2
    Please note that this is like a Java nested class, not like an inner class. An inner class is implicitly associated with a particular instance of the outer class, whereas nested classes exist independently. Commented Jan 8, 2015 at 16:42

1 Answer 1

3

If you press Shift+Ctrl+C while you are inside your class interface declarations Delphi will automatically generate missing methods. In your case those are:

{ TResolutionSet.TResolutionLevel }

constructor TResolutionSet.TResolutionLevel.Create(_discardLayers: Integer; _dims: TRect);
begin

end;

function TResolutionSet.TResolutionLevel.getResolutionBounds: TRect;
begin

end;

function TResolutionSet.TResolutionLevel.getZoomLevel: Integer;
begin

end;

function TResolutionSet.TResolutionLevel.getZoomPercent: Double;
begin

end;

As you can see, you implement the inner class's methods the same way as you implement any other class's methods, by giving the fully qualified name of each method. In the case of a nested class, that means including the name of the outer class(es).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.