unit Unit2;interface
uses
SysUtils;type
TCon = class
private
  i, j : Integer;
public
  function Runtest : string; overload;
  function Runtest(Value : string) : string; overload;end;implementation{ TCon }
function TCon.Runtest: string;
begin
  i := 1;
  while i <= 4 do
  begin
    j := i;
    while j <= 5 do
    begin
      Result := Result + ' ' + IntToStr(j);
    Inc(j);
    end;
  Inc(i);
  end;
end;function TCon.Runtest(Value: string): string;
begin
Result := Value;       
  i := 1;
  while i <= 3 do
  begin
    j := i;
    while j <= 5 do
    begin
      Result := Result + ' ' + IntToStr(j);
    Inc(j);
    end;
  Inc(i);
  end;
end;
end.
unit Unit1;
interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Unit2;type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  ACon : TCon;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  ACon := TCon.Create;
  Memo1.Lines.Text := Edit1.Text + ACon.Runtest;
  ACon.Free;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  ACon := TCon.Create;
  Memo1.Lines.Text := ACon.Runtest(Edit1.Text);
  ACon.Free;
end;{function & Button3}
function Runtest(Value : string) : string;
var
i, j : Integer;begin
Result := Value;
  i := 1;
  while i <= 3 do
  begin
    j := i;
    while j <= (5 + i) do
    begin
      Result := Result + ' ' + IntToStr(j);
    Inc(j, 2);
    end;
  Inc(i);
  end;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
  Memo1.Lines.Text := Runtest(Edit1.Text);
end;
end.本人已把 Unit1 链接 Unit2,Unit2 里有两个函数,而Unit1 有一个自身函数和三个控件。
1 - 关于Overload的问题:
1.1. 如果这样的情况下在Unit2 用Overload 指令对吗?
1.2. 有什么负面效果?
1.3. 以你的建议,你会怎样以最好的方法使用Overload?2 - 关于Result:
2.1. Unit2 里的『function TCon.Runtest: string;』和『function TCon.Runtest(Value: string): string;』
的Result,它们有什么分别??
2.2. 每个Function 里是不是都附带着Result(回函值)?
2.3. Result := Value; <--这是什么,它是什么时候才出现?
2.4. while j <= 5 do
       begin
       Result := Result + ' ' + IntToStr(j);  <--这个 Result(回函值)是不是属于 while?3 - 关于Unit1 的Button3 自身函数:
3.1. 我要怎样在里面加入 Constructor 和 Destructor?『请给例子』
3.2. Create 是属于 Constructor 的值吗?
3.3. Free 是属于 Destructor 的值吗?
3.4. 如果不用 Constructor 和 Destructor 会有什么负面效果出现?

解决方案 »

  1.   

    你这么多问题。才40分啊。我可是个唯利是图的人。
    有点不值当。回答起来很容易,可是要你更容易接受,还是费劲。
    我delphi装虚拟机里面,拷代码都费劲,
    过会儿有时间给你说。
    分太少啊。
    尴尬
      

  2.   

    >.<;;
    等下补多 40分给你,
    其实我的实在不够用。。
      

  3.   

    1.1. 如果这样的情况下在Unit2 用Overload 指令对吗?  对啊,重载当然要Overload了,这个问题有点奇怪。
    1.2. 有什么负面效果?                                 因为可以,所以没有负面效果。
    1.3. 以你的建议,你会怎样以最好的方法使用Overload?  
    需要用到时候才用啊。
    Overload即重载。作用是可以定义参数类型不相同或参数个数不同但名称相同的函数或过程, 
    例如你的
    function Runtest : string; overload;
    function Runtest(Value : string) : string; overload;
    前者无参数,后者有一个参数。
    重载也可用在子类继承父类的时候,子类中重载(注意,非覆盖)父类的方法,但在同一个类中是不能重载
    Published中的函数或过程的。
    例如
     published 
            function   Func(P:   Integer):   Integer;overload; 
            function   Func(P:   Boolean):   Integer;overload; 
    编译器会报错.
    published关键字是为了RTTI的方便使用才提出来的,是专门用来声明属性、事件的,这样,我们就可以在Design-Time对属性、事件进行处理;而不是用来声明函数、过程,它有它的专门作用。
    所以无法在Published中进行重载。
      

  4.   

    2.1. Unit2 里的『function TCon.Runtest: string;』和『function TCon.Runtest(Value: string): string;』
    的Result,它们有什么分别??
    没有区别,都是指最终返回的结果
    2.2. 每个Function 里是不是都附带着Result(回函值)?
    是的
    2.3. Result := Value; <--这是什么,它是什么时候才出现?
    Value是你的参数,function TCon.Runtest([color=#FF0000]Value: string): string;它什么时候出现取决于你,但至少一个函数在运行完以后必须要有一个结果result[/color]
    2.4. while j <= 5 do
      begin
      Result := Result + ' ' + IntToStr(j); <--这个 Result(回函值)是不是属于 while?
    不是,属于你的function。
      

  5.   

    选错了颜色
    2.1. Unit2 里的『function TCon.Runtest: string;』和『function TCon.Runtest(Value: string): string;』
    的Result,它们有什么分别??
    没有区别,都是指最终返回的结果2.2. 每个Function 里是不是都附带着Result(回函值)?
    是的
    2.3. Result := Value; <--这是什么,它是什么时候才出现?
    Value是你的参数,function TCon.Runtest(Value: string): string;它什么时候出现取决于你,但至少一个函数在运行完以后必须要有一个结果result
    2.4. while j <= 5 do
      begin
      Result := Result + ' ' + IntToStr(j); <--这个 Result(回函值)是不是属于 while?
    不是,属于你的function。
      

  6.   

    1 - 关于Overload的问题:
    1.1. 如果这样的情况下在Unit2 用Overload 指令对吗?
    答:对的。
    1.2. 有什么负面效果?
    答:没有。
    1.3. 以你的建议,你会怎样以最好的方法使用Overload?
    答:重载方法应该是根据不同的参数做相同的事情,他们之间往往会互相调用。2 - 关于Result:
    2.1. Unit2 里的『function TCon.Runtest: string;』和『function TCon.Runtest(Value: string): string;』
    的Result,它们有什么分别??
    答:Result变量存储函数返回值,每个函数都有一个Result。也可以用相应的函数名称代替。
    2.2. 每个Function 里是不是都附带着Result(回函值)?
    答:同上。
    2.3. Result := Value; <--这是什么,它是什么时候才出现?
    答:这是赋一个返回值,此时函数返回值即为Value。根据需要,函数体内随时都可以出现。
    2.4. while j <= 5 do
      begin
      Result := Result + ' ' + IntToStr(j); <--这个 Result(回函值)是不是属于 while?
    答:Result属于当前函数,其他任何结构没有所谓的回函值。
    (补充:类似C++的Return(x),在D2009以前的版本中这样写“Result := x; Exit;”,在D2009以后的版本中可以这样写“Exit(x);”)3 - 关于Unit1 的Button3 自身函数:
    3.1. 我要怎样在里面加入 Constructor 和 Destructor?『请给例子』
    答:只有类才有构造(Constructor)、析构(Destructor)函数。
    3.2. Create 是属于 Constructor 的值吗?
    答:构造函数一般就是Constructor Create(...)。
    3.3. Free 是属于 Destructor 的值吗?
    答:在Free方法里会调用析构函数Destructor Destroy。只有一个子方法才会有属于某个方法的说法。
    3.4. 如果不用 Constructor 和 Destructor 会有什么负面效果出现?
    答:每个类都有Constructor 和 Destructor,子类根据需要选择重载或不重载,无“负面”可言。功课做完了。
      

  7.   

    3 - 关于Unit1 的Button3 自身函数:
    3.1. 我要怎样在里面加入 Constructor 和 Destructor?『请给例子』
    Constructor和Destructor是构造函数和析构函数
    自己编写一个类的时候,需要写Constructor和Destructor
    创建类的时候,首先会调用的时候Constructor,
    例如你的ACon := TCon.Create;
    Create就是Constructor Create(Aowner:TComponent)

    3.2. Create 是属于 Constructor 的值吗?
    不是,一般来说Constructor一般命名为Create
    例如 Constructor Create(Aowner:TComponent)

    3.3. Free 是属于 Destructor 的值吗?
    Free是Free,Destructor是Destructor
    procedure Free;DescriptionUse Free to destroy an object. Free automatically calls the destructor if the object reference is not nil. Any object instantiated at runtime that does not have an Owner should be destroyed by a call to Free, so that can be properly destroyed and the memory released. Unlike Destroy, Free is successful even if the object is nil, so if the object was never initialized, Free won't result in an error.When you call Free for a component, it calls Free for all components that it owns, that is, all components in its component list. A form owns all the controls and non-visual components that are created on it in design mode. When it is freed, all of these components are automatically freed as well. Since, by default, all forms are owned by the Application object, when the application terminates, it frees the Application object, which frees all forms. For all objects that are not components, or for components created with a nil owner, be sure to call Free after you are finished with the object; otherwise the allocated memory will not be usable until after the application terminates.
    free的时候,如果对象存在那么自动调用destructor,如果不存在,也不会报错。
    以上是Delphi help中的详细解说,英文你会的。

    3.4. 如果不用 Constructor 和 Destructor 会有什么负面效果出现?
    如果不用Constructor相当于你不写ACon := TCon.Create;
    你觉得不写这句会怎么样?连对象实例都没有,你根本做不了任何事情了。
    如果不用Destructor相当于你不去销毁这个对象实例,它会滞留在内存中。

    累死人,我说的不好。还是自己看书,看源码吧。
    尴尬。走人,被老大看到我在这里无聊了
      

  8.   


    {function & Button3}
    function Runtest(Value : string) : string;
    var
    i, j : Integer;begin
    Result := Value;
      i := 1;
      while i <= 3 do
      begin
        j := i;
        while j <= (5 + i) do
        begin
          Result := Result + ' ' + IntToStr(j);
        Inc(j, 2);
        end;
      Inc(i);
      end;
    end;
    那么每个函数都会附带一个 返回值。
    为什么上面代码函数会有三个返回值?? 
    可以解释它们功用和怎么得到的吗?
      

  9.   

    问题还真多哦。
    这要写多少字才能说清楚啊,累哦。先说说基础的东西吧。
    函数到底是什么?
    在程序中,我们给函数起名称,写函数的参数和返回值,然后去实现函数体,那么,编译的时候,编译器是怎么处理的呢?
    在Delphi中,编译一个单元中的函数,首先获得单元的名称,然后获得函数的名称,然后得到参数列表和返回值,把这些内容融合到一起,作为函数的标识(注意,是编译器自己用来标识一个函数的方式),然后编译函数体,函数体会被编译到一个相对固定的位置,也就是说,每一个函数都有一个入口地址,当你在程序中调用某个函数的时候,实际上就是将程序的流程跳入到这个入口地址中继续执行(在一个线程中,代码只能顺序地一条一条地执行),直到函数结束的时候再跳回到调用函数的地方继续执行。
    简单说,函数就是这个样子。好了,再说说overload。
    从上面的描述中可以看到,一个函数在编译的时候,会被加入单元名称作为其标识的一部分,因此,不同单元中如果出现同名函数,不会有冲突,如果某个单元引用了两个单元,其中这两个单元中有同名函数,则在使用的时候,只要通过 单元名称.函数名称  的方式就能正确调用到函数,如果仅仅使用函数名称,则会调用单元引用列表中,最后一个单元定义的那个函数。
    不同单元中存在同名函数的问题是这样处理的,那么,同一个单元中出现同名函数如何处理呢?
    那就是overload关键字的作用了。
    delphi规定,在一个单元中,不允许出现函数名称、参数列表都一样的函数。如果函数名称相同,但参数列表不一样,则是有条件地允许出现,并且用overload关键字来区分。
    从开始的描述中知道,编译器不但要通过函数的名称,还要结合着参数列表来生成函数的标识,因此,同名函数不同参数实际上被编译器生成了两个不一样的标识(因为参数列表不一样),因此不会冲突。好了,通过上面的描述,你的第一个大问题应该没有问题了下面说说返回值。
    其实返回值也很好理解的,你可以把返回值理解成函数的一个参数,类似这样:
    function TCon.Runtest: string;
    等价于
    procedure TCon.Runtest(Self: TCon; var Result: String);
    其中self是一个隐含参数,所有的类中的方法都会有这么一个隐含参数,而result就相当于返回值了。
    把函数这么等价一下,你的第二个问题,也不是什么问题了,呵呵
    至于
    function TCon.Runtest(Value: string): string;

    function TCon.Runtest: string;
    有什么差别,那差别就是
    procedure TCon.Runtest(Self: TCon; Value: string; var Result: String);

    procedure TCon.Runtest(Self: TCon; var Result: String);
    的差别
    至于你说的Result属于是否属于where这个问题,在Delphi中,变量必须在函数的begin前声明,所有的变量都是在函数的整个函数体重都有效,不会出现某个变量仅仅在某个语句模块中有效地情况(C++可以在函数中定义变量,其变量的作用域可能属于某个语句模块,例如 for(int i = 0; i < 100; i++){...} 这个i就只在for语句模块中有效,出了for这个模块,i就无效了),因此result在函数体中的任何地方都是有效的。
    这是变量作用域的问题,我好像在哪个帖子中回复过这类问题,我找找看 多线程中哪些变量需要同步就是上面这个帖子中我的回复了,虽然说的是多线程的事情,但是其中也有关于变量作用域的相关说明,这类就不再重复打字了。再说说Constructor和Destructor,这是在定义类的一个创建函数和释放函数的关键字,仅仅是关键字而已,是delphi自己的规则,你必须遵守这样的规则去定义类,就好比你必须靠右侧开车,红灯停绿灯行一样,规则而已。当然规则下面会隐藏着原因的,被Constructor关键字定义的函数,会被编译器插入一些代码用来完成类的创建,同样被Destructor定义的函数也会被编译器插入一些代码用来完成一个对象实例的释放。
    至于Create和Free属于谁?这个根本就是外行提出的问题,Create和Free仅仅是函数名称而已,和 Constructor 和 Destructor这两个关键字没有什么关系,仅仅是默认情况下,我们通常用Create名称来定义一个类的创建函数,用 Destroy 来定义一个类的释放函数,这仅仅是习惯而已
    TForm的一个祖先类TCustomForm就通过constructor关键字定义了一个新的创建类的函数CreateNew
    至于Destroy (看好了不是Free呵呵),因为TObject中,这个函数是个虚函数,好多基础类都重载了这个函数,因此,如果你自己定义的释放函数的名字不叫Destroy ,那么你的子类在释放自己的时候会比较麻烦,所以大家在定义释放函数的时候,都是重载Destroy 然后在定义,不过,这也仅仅是习惯上这样而已。
    好了,基本上解释了你的问题。
      

  10.   

    太深了。。 我的基础还没到达这里。。
    其实我正在看基础的书,只是網友们教的太快了 >.<;;