Oop 帕斯卡';这';参考
我开始用面向对象的方法研究Pascal。我想知道是否存在对当前对象的Oop 帕斯卡';这';参考,oop,this,pascal,freepascal,Oop,This,Pascal,Freepascal,我开始用面向对象的方法研究Pascal。我想知道是否存在对当前对象的this或self引用?我到处寻找文件,但没有找到答案 编辑 通过反复试验,我发现您可以使用self。现在我的问题是,我能在编译器不抛出“重复标识符错误”的情况下实现这个(下面的代码段)吗 constructor Employee.create(name:String; salary:Real); begin self.name := name; self.salary := salary; end; 我在下面有一个程
this
或self
引用?我到处寻找文件,但没有找到答案
编辑通过反复试验,我发现您可以使用
self
。现在我的问题是,我能在编译器不抛出“重复标识符错误”的情况下实现这个(下面的代码段)吗
constructor Employee.create(name:String; salary:Real);
begin
self.name := name;
self.salary := salary;
end;
我在下面有一个程序,创建2名员工并显示他们的信息
{$mode objfpc} // directive to be used for defining classes
{$m+} // directive to be used for using constructor
program EmployeeTest;
type
Employee = class
private
name:String;
salary:Real;
public
constructor create(name:String; salary:Real);
procedure setName(name:String);
function getName():String;
procedure setSalary(salary:Real);
function getSalary():Real;
procedure displayEmployee;
end;
var empl1,empl2:Employee;
constructor Employee.create(name:String; salary:Real);
begin
setName(name);
setSalary(salary);
end;
procedure Employee.setName(name:String);
begin
self.name := name;
end;
procedure Employee.setSalary(salary:Real);
begin
self.salary := salary;
end;
function Employee.getName():String;
begin
getName := self.name;
end;
function Employee.getSalary():Real;
begin
getSalary := self.salary;
end;
procedure Employee.displayEmployee;
begin
writeln('Name: ',getName,', Salary: $',getSalary:0:2);
end;
begin
empl1 := Employee.create('Bob', 75000);
empl2 := Employee.create('Joe', 50000);
empl1.displayEmployee();
empl2.displayEmployee();
readln; {pause}
end.
没想到我会回答我的问题 找到了答案
如果要在嵌套函数中复制变量名,必须使用
{$mode delphi}
而不是{$mode objfpc}
。没有想到我会回答我的问题
找到了答案
如果要在嵌套函数中复制变量名,必须使用{$mode delphi}
而不是{$mode objfpc}
type
TEmployee = class(TObject)
private
FName: String;
FSalary:Real;
public
constructor Create(AName:String; ASalary:Real);
property Name: String read FName write FName;
property Salary: Real read FSalary write FSalary;
procedure DisplayEmployee;
end;
constructor TEmployee.Create(AName:String; ASalary:Real);
begin
inherited Create;
FName := AName;
FSalary := ASalary;
end;
procedure TEmployee.DisplayEmployee;
begin
WriteLn('Name: ', FName, ', Salary: $', FSalary:0:2);
end;
type
TEmployee = class(TObject)
private
FName: String;
FSalary:Real;
public
constructor Create(AName:String; ASalary:Real);
property Name: String read FName write FName;
property Salary: Real read FSalary write FSalary;
procedure DisplayEmployee;
end;
constructor TEmployee.Create(AName:String; ASalary:Real);
begin
inherited Create;
FName := AName;
FSalary := ASalary;
end;
procedure TEmployee.DisplayEmployee;
begin
WriteLn('Name: ', FName, ', Salary: $', FSalary:0:2);
end;
请发布您收到的确切错误消息,以及导致错误的行号。您的信息就在眼前,我们看不到您的屏幕或无法读懂您的心思。这是错误消息,但感谢您的输入。我找到了答案。c:\Pascal\2.6.0\code>fpc Employee.pas免费Pascal编译器版本2.6.0[2011/12/25]for i386版权所有(c)Florian Klaempfl和其他人目标OS:Win32 for i386编译Employee.pas Employee.pas(11,24)错误:重复标识符“name”Employee.pas(11,37)错误:重复标识符“salary”Employee.pas(58)致命错误:编译模块时出现2个错误,正在停止致命错误:编译中止错误:C:\Pascal\2.6.0\bin\i386-Win32\ppc386.exe返回一个错误ExitDeployment请发布您收到的确切错误消息以及导致该错误的行号。您的信息就在眼前,我们看不到您的屏幕或无法读懂您的心思。这是错误消息,但感谢您的输入。我找到了答案。c:\Pascal\2.6.0\code>fpc Employee.pas免费Pascal编译器版本2.6.0[2011/12/25]for i386版权所有(c)Florian Klaempfl和其他人目标OS:Win32 for i386编译Employee.pas Employee.pas(11,24)错误:重复标识符“name”Employee.pas(11,37)错误:重复标识符“salary”Employee.pas(58)致命错误:编译模块时出现2个错误,正在停止致命错误:编译中止错误:C:\Pascal\2.6.0\bin\i386-Win32\ppc386.exe返回一个错误ExitCode谢谢您的输入。我知道这是Pascal的惯例,但我尝试使用accessor/mutators模拟Java/Ruby OOP。谢谢您的输入。我知道这是Pascal的惯例,但我试图通过使用访问器/变异器来模拟Java/Ruby OOP。