unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormClick(Sender: TObject);
  private
    Persons:array[1..4] of TPerson;
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
   Persons[1]:TBoss.Create('赵晓光',800.0);
   Persons[2]:TEmployee.Create('钱明',400.0,3.0,150);
   Persons[3]:TPieceWorker.Create('孙正',2.5,200);
   Persons[4]:THourlyWorker.Create('李大',13.75,50.0);
end;procedure TForm1.FormDestory(Sender:TObject);
var
  i:Integer;
begin
  for i:=1 to 4 do begin
     Persons[i].Free;
     Persons[i]:=nil;
  end;
end;procedure TForm1.FormClick(Sender: TObject);
var
  i:Integer;
begin
  for i:=1 to 4 do
    ListBox1.Items.Add(Format('%8s %10.2f',[Person[i].Name,Person[i].Earnings]));
end;end.
unit untPerson;interfacetype
  TPerson=class
  private
    FName:string;
  public
    constructor Create(NameValue:string);
    function Earnings:Real;virtual;abstract;
    property Name:string read FName write FName;
  end;implementationconstructor TPerson.Create(NameValue:string);
begin
   FName:=NameValue;
end;
end.type
  TBoss=class(TPerson)
  private
    FSalary:Real;
  public
    constructor Create(NameValue:string;SalaryValue:Real);
    function Earnings:Real;override;
    property WeeklySalary:Real read FSalary write FSalary;
  end;
implementationuses
  SysUtils;constructor Create(NameValue:string;SalaryValue:Real);
begin
   inherited Create(NameValue);
   WeeklySalary:=SalaryValue;
end;function TBoss.Earnings:Real;
begin
   Result:=WeeklySalary;
end;
end.
unit untPieceWork;interfaceuses
   untPerson;type
   TPieceWorker=class(TPerson)
   private
      FQuantity:Integer;
      FWagePerPiece:Real;
   public
     constructor Create(NameValue:string;WagePerPieceValue:Real;QuantityValue:Integer);
     function Earnings:Real;override;
     property WagePerPiece:Real read FWagePerPiece write FWagePerPiece;
     property Quantity:Real read FQuantity write FQuantity;
   end;implementationconstructor Create(NameValue:string;WagePerPieceValue:Real;QuantityValue:Integer);
begin
   inherited Create(NameValue);
   WagePerPiece:=WagePerPieceValue;
   Quantity:=QuantityValue;
end;function TPieceWorker.Earnings:Real;
begin
   Result:=WagePerPiece*Quantity;
end;
end.unit untEmployee;interfaceuses
   untPerson;type
   TEmployee=class(TPerson)
   private
      FQuantity:Integer;
      FWagePerItem:Real;
      FSalary:Real;
   public
      constructor Create(NameValue:string;SalaryValue,WageperItemValue:Real;QuantityValue:Integer);
      function Earnings:Real;override;
      property WeeklySalary:Real read FSalary write FSalary;
      property WagePerItem:Real read FWagePerItem write FWagePerItem;
      property Quantity:Integer read FQuantity write FQuantity;
   end;implementationconstructor Create(NameValue:string;SalaryValue,WageperItemValue:Real;QuantityValue:Integer);
begin
   inherited Create(NameValue);
   WeeklySalary:=SalaryValue;
   WageperItem:=WageperItemValue;
   Quantity:=QuantityValue;
end;function TEmployee.Earnings:Real;
begin
   Result:=WeeklySalary+WagePerItem*Quantity;
end;
end.
unit untHourlyWorker;interfaceuses
   untPerson;type
   THourlyWorker=class(TPerson)
   private
      FHourWorked:Real;
      FWage:Real;
   public
     constructor Create(NameValue:string;WageValue,HourWorkedValue:Real);
     function Earnings:Real;override;
     property Wage:Real read FWage write FWage;
     property HourWorked:Real read FHourWorked write FHourWorked;
   end;implementationconstructor Create(NameValue:string;WageValue,HourWorkedValue:Real);
begin
   inherited Create(NameValue);
   Wage:=WageValue;
   HourWorked:=HourWorkedValue;
end;function THourlyWorker.Earnings:Real;
begin
   if HoursWorked<=40 then
     Result:=Wage*HoursWorked;
   else
     Result:=Wage*40+Wage*1.5*(HoursWorked-40);
end;
end.运行时错误提示如下:
[Error] untBoss.pas(22): Undeclared identifier: 'Create'
  [Error] untBoss.pas(24): This form of method call only allowed in methods of derived types
  [Error] untBoss.pas(24): Not enough actual parameters
  [Error] untBoss.pas(25): Undeclared identifier: 'WeeklySalary'
  [Error] untBoss.pas(13): Unsatisfied forward or external declaration: 'TBoss.Create'
  [Fatal Error] Project1.dpr(9): Could not compile used unit 'untBoss.pas'

解决方案 »

  1.   

    在implementation下,Create函数头部应当加上类名:
    constructor 类名.Create
    比如constructor THourlyWorker.Create(NameValue:string;WageValue,HourWorkedValue:Real);
      

  2.   

    program Project1;uses
      Forms,
      untPerson in 'untPerson.pas' {Form1},
      untBoss in 'untBoss.pas',
      untEmployee in 'untEmployee.pas',
      untPieceWork in 'untPieceWork.pas',
      untHourlyWorker in 'untHourlyWorker.pas';{$R *.res}begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    这种问题解决了,可是竟然出现了另外一个问题
      [Error] Project1.dpr(15): Undeclared identifier: 'TForm1'
      

  3.   

    program Project1;uses
      Forms,
      untPerson in 'untPerson.pas' {Form1},
      untBoss in 'untBoss.pas',
      untEmployee in 'untEmployee.pas',
      untPieceWork in 'untPieceWork.pas',
      untHourlyWorker in 'untHourlyWorker.pas';{$R *.res}begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    那个问题解决了,但是出了新的问题:
    [Error] Project1.dpr(15): Undeclared identifier: 'TForm1'
      

  4.   

    你是不是把form1的name给改了啊
      

  5.   

    不对  我改的不是Name属性  是Caption属性
      

  6.   

    改成这样:
    uses
      Forms,
      untPerson in 'untPerson.pas',
      untBoss in 'untBoss.pas',
      untEmployee in 'untEmployee.pas',
      untPieceWork in 'untPieceWork.pas',
      untHourlyWorker in 'untHourlyWorker.pas',
      Unit1 in 'Unit1.pas' {Form1};
      

  7.   

    我这么改了  但是还是有问题
    [Error] Project1.dpr(10): Declaration expected but identifier 'Unit1' found
    [Error] Project1.dpr(15): Undeclared identifier: 'TForm1