質問

I am in the progress of trying to learn a few new tricks in order to better organize some of the sourcecode in my units in Delphi. I have noticed that some functions or methods I access appear to be classes inside classes, but I have not yet been successful in making a working class inside a class, although it compiles fine, I still get error messages when executing the code.

What I would like to do is to make kind of a tree with functions, procedures and values to set or get. I would be grateful if somebody could help me out a little bit with an example. I have today some classes that are Types. I then assign the types to a variable: something=TSomething and Then for something to happen I write "something.action".

My aim is to go further, and define sub-functions or/and sub-procedures. Lets say I have three or four classes. TSnippet, TAction1, TAction2, TSubAction1, Etc. I would like to use or assign these to a single variable and use them like:

Snippet.Action1.SubAction1.Organize(param1,param2);
Snippet.Action2.SubAction2.Returns='SomeString';
Snippet.Action1.SubAction1.SomeProcedure;

Is anybody able to help me with a useful example as in how to write code for this approach to work? And also.. does anybydy know how such an implementation of code will affect CPYCycles needed in order to execute code versus the old fashioned method of having thousands of procedures with all different names, but more direct access (it feels like more direct access).

As of my first text was maybe a bit unclear, this follows. I would like to make use of the editors automatic suggestions of procedures/functions available in order to simplify programming a little bit. I started to make a Class for this, and it works great. Consider a classname "Data". What can we do with data? We can Edit, Add, Delete, Save and Load. Ok. This is my first Class.

Consider then another Class: "Encrypt". We can do DES, DES3, HASH, BITSHUFFLE. I can go on with a third Class: "Compress". We can do LZW, ZIP, RAR, MP3, MPG, MP4, etc.

Instead of using these as 3 different classes, I would like to combine them in one, yet keeping them separate. I would like to make a kind of an OwnerClass for the other classes. We can call this "MyStuff"

Whenever I type "MyStuff." in the editor, I should get up a list of "Data, Encrypt, Compress". Further, When I then choose "Compress", the next list for that class' procedures and functions will list up.

The classes may have some local variables, but the main functionality will be towards global arrays.

Maybe there are other ways of achieving this. I don't know. My basic aim is to be able to categorize and put together routines that belong together. This is already done in Units, but this does not help with the automatic list from the editor. Thank you in advance. Morten.

役に立ちましたか?

解決

I think I understand what it is you're asking, after your edit.

What you're calling "classes in classes" are called properties and methods; they're other classes, variables, or procedures/functions that are declared in their containing class. The "list" you're talking about is called Code Insight, and it shows you the available properties and methods of the class you're referencing in your code at that point.

This should do something like you describe, and give you an idea of how to implement it in your own code:

unit MyStuffUnit;

interface

uses
  SysUtils;

type

  TEncryptionType = (etDES, etDES3, etHASH, etBITSHUFFLE);

  TMyStuffEncryption = class(TObject)
  private
    FEncryptType: TEncryptionType;
  public
    constructor Create;
  published
    property EncryptionType: TEncryptionType read FEncryptType
      write FEncryptType;
  end;

  TCompressionType = (ctLZW, ctZIP, ctRAR, ctMP3, ctMPG, ctMP4);

  TMyStuffCompression = class(TObject)
  private
    FCompressionType: TCompressionType;
  public
    constructor Create;
  published
    property CompressionType: TCompressionType read FCompressionType
      write FCompressionType;
  end;

  TMyStuff = class(TObject)
  private
    FCompression: TMyStuffCompression;
    FEncryption: TMyStuffEncryption;
  public
    constructor Create;
    destructor Destroy; override;
  published
    property Compression: TMyStuffCompression read FCompression
      write FCompression;
    property Encryption: TMyStuffEncryption read FEncryption
      write FEncryption;
  end;

implementation

constructor TMyStuffEncryption.Create;
begin
  inherited;
  FEncryptType := etDES;
end;

constructor TMyStuffCompression.Create;
begin
  inherited;
  FCompressionType := ctLZW;
end;

constructor TMyStuff.Create;
begin
  inherited;
  FCompression := TMyStuffCompression.Create;
  FEncryption := TMyStuffEncryption.Create;
end;

destructor TMyStuff.Destroy;
begin
  FCompression.Free;
  FEncryption.Free;
  inherited;
end;

end.

If you create an instance of TMyStuff in your code, you should be able to type MyStuff. and get the option of choosing Compression or Encryption. Choosing Compression should allow you to set the CompressionType property.

enter image description here

This should be enough to get you going. :-) Remember that Delphi includes the source code for the VCL and RTL in almost all editions, so you always have that to look at for examples. (They're not always the very best examples, but they should give you ideas on how to do things.)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top