TMydefault = class(TObject)
private
protected function Get(Index: Integer):TMyType; virtual; abstract;
procedure Put(Index: Integer;const Value:TMyType);virtual;
public
FList:array [0..254] of TMyType; constructor Create;
destructor Destroy; override; property List[Index: Integer]: TMyType read Get write Put;default;
published end;procedure TMydefault.Put(Index: Integer;const Value:TCheckAndText);
begin
FList[Index]:= Value;
end;TStrings;
function Get(Index: Integer): string; virtual; abstract;
procedure Put(Index: Integer; const S: string); virtual;property Strings[Index: Integer]: string read Get write Put; default;procedure TStrings.Put(Index: Integer; const S: string);
var
TempObject: TObject;
begin
TempObject := GetObject(Index);
Delete(Index);
InsertObject(Index, S, TempObject);
end;
为什么 我的就不行?
如果就像现在这么写的,Get是个没有被继承的抽象方法,如果访问抽象方法的法,自然会报abstract错误
[Error] Left side cannot be assigned to
TMydefault = class(TObject)
private
protected function Get(Index: Integer):TMyType; virtual; abstract;
procedure Put(Index: Integer;const Value:TMyType);virtual;
public
FList:array [0..254] of TMyType; constructor Create;
destructor Destroy; override; property List[Index: Integer]: TMyType read Get write Put;default;
published end;procedure TMydefault.Put(Index: Integer;const Value:TMyType);
begin
FList[Index]:= Value;
end;
这个用法够有创意的
按照c语言常用的概念,delphi的属性是个左值(lvalue),通俗点儿讲,不能被取地址的值。一个不能被取地址的值,你访问它的成员变量当然是不可能了
也就是说,你只能先定义一个TMyType的变量,然后整体把TMyType给property,而不可能只把TMyType的成员变量的值给property
TStrings 怎么又可以?
你在哪裏看到可以了!你自己定的義TMYCLIASS的數組在哪裏?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
Strings:TStrings;
begin
Strings:= TStrings.Create; Strings.Add('我日');
Strings[0]:='我再日';
ShowMessage(Strings[0]);
Strings.Free;end;end.运行抱错 abstract
编译能过
怎么我的编都编不过
是不是还要派生一个类才行?
我要实现和 TStringList一样的效果该怎么写?
就是 实例名[Index] 直接访问 这个实例的list的第Index个对象
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TMyType = class
A : integer;
b : integer;
end;
type
TMydefault = class(TObject)
private
protected function Get(Index: Integer):TMyType; virtual;
procedure Put(Index: Integer;const Value:TMyType);virtual;
public
FList:array [0..254] of TMyType; constructor Create;
destructor Destroy; override; property List[Index: Integer]: TMyType read Get write Put;default;
published end;type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;
vDef : TMydefault;
implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
var
v : TMyType;
begin
v := TMyType.Create;
v.A := 10;
v.b := 20;
vDef[0] := V;
end;procedure TForm2.Button2Click(Sender: TObject);
begin
showMessage(Inttostr(vdef[0].A));
end;procedure TForm2.FormCreate(Sender: TObject);
begin
vDef := TMydefault.Create;
end;{ TMydefault }constructor TMydefault.Create;
beginend;destructor TMydefault.Destroy;
begin inherited;
end;function TMydefault.Get(Index: Integer): TMyType;
begin
Result := FList[index];
end;procedure TMydefault.Put(Index: Integer; const Value: TMyType);
begin
FList[Index] := Value;
end;end.
type
TMyType = class
A : integer;
b : integer;
end;
TMyType 不能是 record 吗 ?type
TMyType = record
A : integer;
b : integer;
end;
type
TMyType = class
A : integer;
b : integer;
end;
TMyType 不能是 record 吗 ?type
TMyType = record
A : integer;
b : integer;
end;
还是我那个样
vDef[0] := V;
这样可以 vDef[0].a 就不行 是不是肯定不行的
实在不行我就用函数算了
那就用两个函数吧
一个读一个写
get(index).a