面向对象的菜鸟级问题(300分)

  • 主题发起人 drawpharos
  • 开始时间
D

drawpharos

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在设计一个对象TRadius,其中包含一个TRadiusPackages对象,
TRadiusPackages对象中又包括TAttributes对象,([red]是包含,不是继承[/red])
TAttributes应有很多的Attrubite,并且要实现insert,delete,find等操作,
这几个对象该怎么设计?主要是TAttributes应如何设计?
下面是我的源代码,但我觉得TAttributes对象设计的有问题,请大家领评判
代码:
unit Radius;

interface

type
  TRadiusAttribute = class //Radius属性对象
  private
    {以下是TRadiusAttribute的一些属性}
    AttrType: Byte;
    length : Byte;
    DataType : string;
    value : TMemoryStream;
    Prev,Next : TRadiusAttribute;
    {以下是TRadiusAttribute的一些方法}
    procedure Create;
    procedure Append(AttrType : Byte
DayaType , Value : String);
    procedure Insert(Index : Byte
AttrType : Byte
DayaType , Value : TMemoryStream);
    procedure Modyfy(Index : Byte
AttrType : Byte
DayaType , Value : TMemoryStream);
    procedure Delete(Index : Byte)
overload;
    procedure Delete(AttrType : Byte
DayaType , Value : TMemoryStream)
overload;
    function find(AttrType : Byte
DayaType , Value : TMemoryStream) : Byte;
    function Next : TRadiusAttribute;
    function Prev : TRadiusAttribute;
  end;

type
  TRadiusPackage = class //Radius数据包对象
  private
    Code: Byte;
    Identifier : Byte;
    length : Word;
    Authenticator : Array [0..15] of Byte;
    Attributes : TRadiusAttribute;
    PackageStream : TMemoryStream;
  public
  end;

  TRadius = class //Radius协议对象
  private
    AuthPort :Word
 //认证端口
    AccPort : Word
 //计费端口
    Package: TRadiusPackage;

  end;

implementation

end.
 
X

xianjun

Unregistered / Unconfirmed
GUEST, unregistred user!
TAttribute = class(TCollectionItem)
...

TAttributes = class(TOwnedCollection)
...
public
constructor Create(AOwner: TPersistent);
...

constructor TAttributes.Create(AOwner: TPersistent);
begin
inherited Create(AOwner, TAttribute);
end;

insert,delete,find等操作都可以用TOwnedCollection的方法
当然你要继承相关的代码
 
W

wlmmlw

Unregistered / Unconfirmed
GUEST, unregistred user!
没问题,一切都很好^_^
 
D

drawpharos

Unregistered / Unconfirmed
GUEST, unregistred user!
但是我怎么来定义才能让[red]TCollection.Item[/red]就是[blue]TAttribute[/blue]呢?
我可不可以定义
unit Radius;
interface
type
TRadiusAttribute = class (TCollectionItem)//Radius属性对象
private
{以下是TRadiusAttribute的一些属性}
AttrType: Byte;
length : Byte;
DataType : string;
value : TMemoryStream;
Prev,Next : TRadiusAttribute;
{以下是TRadiusAttribute的一些方法}
procedure Create;
end;
type
TRadiusPackage = class (TCollection) //Radius数据包对象
private
Code: Byte;
Identifier : Byte;
length : Word;
Authenticator : Array [0..15] of Byte;
Attributes : TRadiusAttribute
[red]//想让Attributes就是TCollection.Item[/red]
PackageStream : TMemoryStream;
end;
TRadius = class //Radius协议对象
private
AuthPort :Word
//认证端口
AccPort : Word
//计费端口
Package: TRadiusPackage;
end;
implementation
end.
 
X

xianjun

Unregistered / Unconfirmed
GUEST, unregistred user!
当然是自己写啦
type
TAttribute = class(TCollectionItem)
end;

TAttributes = class(TOwnedCollection)
private
function GetItems(I: Integer): TAttribute;
procedure SetItems(I: Integer
const Value: TAttribute);
public
constructor Create(AOwner: TPersistent);
property Items[I: Integer]: TAttribute read GetItems write SetItems;
end;

{ TAttributes }

constructor TAttributes.Create(AOwner: TPersistent);
begin
inherited Create(AOwner, TAttribute);
end;

function TAttributes.GetItems(I: Integer): TAttribute;
begin
Result := TAttribute(inherited Items);
end;

procedure TAttributes.SetItems(I: Integer
const Value: TAttribute);
begin
Items.Assign(Value)
//假设你有个Assign方法
end;

 
D

drawpharos

Unregistered / Unconfirmed
GUEST, unregistred user!
谢谢xianjun!能交个朋友吗?
MSN: drawpharos@163.net
QQ: 555507 [验证请写 DFW ]
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
567
import
I
顶部