Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 帕斯卡';这';参考_Oop_This_Pascal_Freepascal - Fatal编程技术网

Oop 帕斯卡';这';参考

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; 我在下面有一个程

我开始用面向对象的方法研究Pascal。我想知道是否存在对当前对象的
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}

  • 不要对参数和属性使用相同的名称。将前缀A用作参数
  • 不要实现getter和setter方法。在Pascal中使用property关键字
  • 将前缀F用于专用字段
  • 将前缀T用于自定义类型
  • 以下是您的班级的一个示例:

    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;
    
  • 不要对参数和属性使用相同的名称。将前缀A用作参数
  • 不要实现getter和setter方法。在Pascal中使用property关键字
  • 将前缀F用于专用字段
  • 将前缀T用于自定义类型
  • 以下是您的班级的一个示例:

    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。