下面的代码怎样理解?承继后,再复盖?(100分)

  • 主题发起人 主题发起人 hsgrass
  • 开始时间 开始时间
H

hsgrass

Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码怎样理解?承继后,再复盖?
T2cccStringGrid = class(TStringGrid)
protected
function CreateEditor: TInplaceEdit
override;
function GetEditStyle(ACol, ARow: Longint): TEditStyle
override;

procedure EditListGetItems(ACol, ARow: Integer
Items: TStrings);
procedure EditButtonClick(Sender: TObject);
end;
TStringGrid = class(T2cccStringGrid)
// 可以正常使用

ttest2 = class(tbutton)
public
procedure Click
override;
end;
tbutton = ttest2
// 出現class tbutton not found

TForm1 = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
 
tbutton = ttest2
// 出現class tbutton not found

use ttest所在的单元了吗?
 
TTEST已經定義在上面
 
function T2cccStringGrid.CreateEditor: TInplaceEdit;
begin
Result := TInplaceEditList.Create(Self);
(Result as TInplaceEditList).OnGetPickListitems := EditListGetItems;
(Result as TInplaceEditList).OnEditButtonClick := EditButtonClick;
end;

function T2cccStringGrid.GetEditStyle(ACol, ARow: Integer): TEditStyle;
begin
case ACol mod 2 of
0: Result := esEllipsis;
1: Result := esPickList;
else
Result := inherited GetEditStyle(ACol, ARow);
end;
end;

procedure T2cccStringGrid.EditListGetItems(ACol, ARow: Integer
Items: TStrings);
begin
case ACol mod 2 of
0: Items.CommaText := '2ccc,delphibox,zizii';
1: Items.CommaText := 'welcome,stringgrid,demo';
end;
end;

procedure T2cccStringGrid.EditButtonClick(Sender: TObject);
begin
Cells[Col, Row] := 'Hello 2ccc';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Options := StringGrid1.Options + [goEditing];
StringGrid1.DefaultRowHeight := 21;
end;
 
tbutton = ttest2;这里写错,应该是 tbutton = class(ttest2);吧
以前看过这种写法都不理解,哈哈,今天总算见识了.
 
是阿,原來可以這樣用滴,謝謝
這兩個類要定義在不同的單元,如果在同一個單元會不能編譯,提示重復定義.
這樣就可以在不同的單元修正其他類的行為 :)
------------------Unit1.pas
type
Ttest1 = class
public
procedure abc
virtual;
end;
ttest = class(Ttest1);
procedure TForm1.btn1Click(Sender: TObject);
var
t: Ttest1;
begin
t := Ttest1.Create;
t.abc;
t.free;
end;


{ Ttest1 }
procedure Ttest1.abc;
begin
ShowMessage('test1');
end;

----------------------------unit Unit2;
interface
uses classes, dialogs;
type
Ttest = class
public
procedure abc
virtual;
end;

implementation
{ Ttest }

procedure Ttest.abc;
begin
ShowMessage('test');
end;

end.
 
接受答案了.
 
If the declaration of a class type ends with the word class and a semicolon--that is, if it has the form

type className = class;

with no ancestor or class members listed after the word class--then it is a forward declaration. A forward declaration must be resolved by a defining declaration of the same class within the same type declaration section. In other words, between a forward declaration and its defining declaration, nothing can occur except other type declarations.

Forward declarations allow mutually dependent classes. For example,

type
TFigure = class
// forward declaration
TDrawing = class
Figure: TFigure;
...
end;
TFigure = class // defining declaration
Drawing: TDrawing;
...
end;

Do not confuse forward declarations with complete declarations of types that derive from TObject without declaring any class members.

type
TFirstClass = class
// this is a forward declaration
TSecondClass = class // this is a complete class declaration
end
//
TThirdClass = class(TObject)
// this is a complete class declaration
幫助有說明的,叫"complete class declaration"(類實現定義),我的理解是這個類的實現,由Tthirdclass完成 :0
 
后退
顶部