为什么运行出错?说read 和 write方法不是基类. ( 积分: 200 )

  • 主题发起人 主题发起人 linuxnc
  • 开始时间 开始时间
L

linuxnc

Unregistered / Unconfirmed
GUEST, unregistred user!
type
Tmystream=class(Tfilestream)
private
fkey:string;
public
constructor create
(const filename:string;mode:word);
function read(var buffer;count:longint):
longint;override;
function write(const buffer;count:longint):
longint;override;
property key:string read fkey write fkey ;
end;

  在Tmystream的声名中,我们对read、write两个方法进行了重载,
并添加了一个新的特性key,用以存储对文件进行加密时所需的密码。
为实现文件读写的加密,在write方法中,将key的每个字符依次与buffer中的字符相加,
将得到的结果写入文件,实现加密;在read方法中,将读出的内容依次与key的每个字符
相减,实现解密。加密及解密的方法多种多样,可以通过改写相关代码,得到不同的
加密方法。
程序清单如下:
function Tmystream.write(const buffer;
count:longint):longint;
var
Pbu,Pmy,mykey:pchar;
i,enc:integer;
begin
getmem(pmy,count); //为pmy分配内存
mykey:=pchar(key); //将key转换为pchar指针
try
pbu:=pchar(@buffer); //将buffer转换为pchar指针
for i:=0 to count-1 do
//将key的每个字符以此与buffer的
每个字符循环相加,结果放入pmy指向的内存区
begin
enc:=(ord(pbu)+ord(mykey
[(i mod length(key))])) mod 256;
Pmy:=char(enc);
end;
result:=inherited write(Pmy^,count);
//将pmy指向的内容写入文件
finally
freemem(Pmy,count);
end;
end;
function Tmystream.read(var buffer;count:longint):
longint;
var
Pbu,Pmy,mykey:pchar;
i,mycount,enc:integer;
begin
getmem(Pmy,count);//为pmy分配内存
mykey:=pchar(key);//将key转换为pchar指针
try
mycount:=inherited read(Pmy^,count);
//将文件内容读入pmy指向内存区
Pbu:=Pchar(@buffer);将buffer转换为pchar指针
for i:=0 to mycount-1 do//将key的每个字符依次
与pmy的每个字符循环相减,结果放入pbu指向的变量
begin
enc:=(ord(Pmy)-ord(mykey
[(i mod length(key))])) mod 256;
Pbu:=chr(enc);
end;
finally
freemem(Pmy,count);
end;
result:=mycount;
end;

  完成定制文件流的编写后,便可在程序中应用,实现文件的读写加密,例程如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls,unit2,unit3;
//unit2定义了Tmystream
//unit3定义了输入密码对话框form3
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Panel1: TPanel;
Panel2: TPanel;
Memo1: TMemo;
Splitter1: TSplitter;
Memo2: TMemo;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
//将选定的加密文件解开,读入memo2
var
encstr:Tmystream;
begin
if opendialog1.Execute and (form3.showmodal=mrok)
then
begin
encstr:=Tmystream.create
(opendialog1.filename,fmopenread);
encstr.key:=form3.Edit1.Text;
try
memo2.lines.LoadFromStream(encstr);
finally
encstr.Free;
end;
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
//将memo1中的内容加密,用指定文件名另存
var
encstr:Tmystream;
begin
if savedialog1.Execute and (form3.showmodal=mrok)
then
begin
encstr:=Tmystream.create(savedialog1.filename,
fmcreate);
encstr.key:=form3.Edit1.Text;
try
memo1.lines.SaveToStream(encstr);
finally
encstr.Free;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
//将指定文件读入memo1
var
mystr:Tfilestream;
begin
if opendialog1.Execute then
begin
mystr:=Tfilestream.create
(opendialog1.filename,fmopenread);
try
memo1.lines.LoadFromStream(mystr);
finally
mystr.Free;
end;
end;
end;
end.
 
type
Tmystream=class(Tfilestream)
private
fkey:string;
public
constructor create
(const filename:string;mode:word);
function read(var buffer;count:longint):
longint;override;
function write(const buffer;count:longint):
longint;override;
property key:string read fkey write fkey ;
end;

  在Tmystream的声名中,我们对read、write两个方法进行了重载,
并添加了一个新的特性key,用以存储对文件进行加密时所需的密码。
为实现文件读写的加密,在write方法中,将key的每个字符依次与buffer中的字符相加,
将得到的结果写入文件,实现加密;在read方法中,将读出的内容依次与key的每个字符
相减,实现解密。加密及解密的方法多种多样,可以通过改写相关代码,得到不同的
加密方法。
程序清单如下:
function Tmystream.write(const buffer;
count:longint):longint;
var
Pbu,Pmy,mykey:pchar;
i,enc:integer;
begin
getmem(pmy,count); //为pmy分配内存
mykey:=pchar(key); //将key转换为pchar指针
try
pbu:=pchar(@buffer); //将buffer转换为pchar指针
for i:=0 to count-1 do
//将key的每个字符以此与buffer的
每个字符循环相加,结果放入pmy指向的内存区
begin
enc:=(ord(pbu)+ord(mykey
[(i mod length(key))])) mod 256;
Pmy:=char(enc);
end;
result:=inherited write(Pmy^,count);
//将pmy指向的内容写入文件
finally
freemem(Pmy,count);
end;
end;
function Tmystream.read(var buffer;count:longint):
longint;
var
Pbu,Pmy,mykey:pchar;
i,mycount,enc:integer;
begin
getmem(Pmy,count);//为pmy分配内存
mykey:=pchar(key);//将key转换为pchar指针
try
mycount:=inherited read(Pmy^,count);
//将文件内容读入pmy指向内存区
Pbu:=Pchar(@buffer);将buffer转换为pchar指针
for i:=0 to mycount-1 do//将key的每个字符依次
与pmy的每个字符循环相减,结果放入pbu指向的变量
begin
enc:=(ord(Pmy)-ord(mykey
[(i mod length(key))])) mod 256;
Pbu:=chr(enc);
end;
finally
freemem(Pmy,count);
end;
result:=mycount;
end;

  完成定制文件流的编写后,便可在程序中应用,实现文件的读写加密,例程如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls,unit2,unit3;
//unit2定义了Tmystream
//unit3定义了输入密码对话框form3
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Panel1: TPanel;
Panel2: TPanel;
Memo1: TMemo;
Splitter1: TSplitter;
Memo2: TMemo;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
//将选定的加密文件解开,读入memo2
var
encstr:Tmystream;
begin
if opendialog1.Execute and (form3.showmodal=mrok)
then
begin
encstr:=Tmystream.create
(opendialog1.filename,fmopenread);
encstr.key:=form3.Edit1.Text;
try
memo2.lines.LoadFromStream(encstr);
finally
encstr.Free;
end;
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
//将memo1中的内容加密,用指定文件名另存
var
encstr:Tmystream;
begin
if savedialog1.Execute and (form3.showmodal=mrok)
then
begin
encstr:=Tmystream.create(savedialog1.filename,
fmcreate);
encstr.key:=form3.Edit1.Text;
try
memo1.lines.SaveToStream(encstr);
finally
encstr.Free;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
//将指定文件读入memo1
var
mystr:Tfilestream;
begin
if opendialog1.Execute then
begin
mystr:=Tfilestream.create
(opendialog1.filename,fmopenread);
try
memo1.lines.LoadFromStream(mystr);
finally
mystr.Free;
end;
end;
end;
end.
 
老兄,read和write是关键字,不能重载!!!
根据你的意思,这样大概就可以了:
Tmystream=class(Tfilestream)
private
fkey:string;
function getkey:string;
procedure setkey(s:string);
public
constructor create
(const filename:string;mode:word);
function read(var buffer;count:longint):
longint;override;
function write(const buffer;count:longint):
longint;override;
property key:string read getkey write setkey ;
end;
只要填写getkey 和setkey就行了!
 
其实把constructor create()这句去掉就行了。你看看,我运行很正常。
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm2 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
//*********************************
type
TMyStream = class(TFileStream)
private
fkey:string;
public
function read(var buffer;count:longint):longint;override;
function write(const buffer;count:longint):longint;override;
property key:string read fkey write fkey ;
end;


//*********************************

var
Form2: TForm2;

implementation

{$R *.dfm}
 
to yeskert1: 谢谢,但我还是看不懂你写的是怎么样,应该也不是你说的问题吧。

to lgmlgm2000 : 谢谢,我试试你说先。你运行正常,应该我也行的吧。我的是 win2k系统。
 
to lgmlgm2000對於其方法確實可行。
按照你得意思其類可以不需要constructor create()這句。
如果是線程就不一樣。
 
create声明处加上overload:
constructor create (const filename:string;mode:word); overload;
 
lgmlgm2000的说法确实可行,delphi兄帮人的实践精神确实令我佩服,多谢。
至于dreamisx 的说法我也试过,也是提示出错的。
多谢大家关注.
 
后退
顶部