unit Unit3;interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TTest=class
protected
ProtectedData:Integer;public
PublicData:Integer;
function GetValue:string;
end;implementationfunction GetValue:string;
begin
Result:=Format ('Public: %d,Protected: %d',[PublicData,ProtectedData]);
end;end.
___________________________________________
问题:
1.类必须放到单元中吗?一个单元可以创建多个类吗?
2.单元跟类什么关系?是类的封装吗?谢谢!!
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TTest=class
protected
ProtectedData:Integer;public
PublicData:Integer;
function GetValue:string;
end;implementationfunction GetValue:string;
begin
Result:=Format ('Public: %d,Protected: %d',[PublicData,ProtectedData]);
end;end.
___________________________________________
问题:
1.类必须放到单元中吗?一个单元可以创建多个类吗?
2.单元跟类什么关系?是类的封装吗?谢谢!!
2.基本没关系;一个类可以分散在多个单元中,一个单元也可以放多个类;可以说是封装
-------
你的类写的有些问题吧
-----
TTest=class
private
FProtectedData:Integer;//如果是内部变量,放在private关键字里面,实现细节封装
FHello: string;//自动产生,当你写了property Hello:string;那句后点ctrl+shift+c
procedure SetHello(const Value: string);//自动产生
protected
//
public
property Hello:string read FHello write SetHello;//属性放public或published里面
function Encr(str:string):string;
published
//
end;
-------
function TTest.Encr(str: string): string;
begin
///
end;procedure TTest.SetHello(const Value: string);
begin
FHello := Value;
end;
太感谢了,小弟初学,还请多多指教!!要分的话,说一声:)
__________________________
上面我写的代码哪不对啊,我可是照书上写的?
[Error] Unit3.pas(22): Undeclared identifier: 'PublicData'
[Error] Unit3.pas(22): Undeclared identifier: 'ProtectedData'