全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:976
推到 Plurk!
推到 Facebook!

關於inherited後面加不加method的問題?

答題得分者是:william
skurama
中階會員


發表:88
回覆:127
積分:73
註冊:2002-07-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-04-23 11:04:12 IP:61.219.xxx.xxx 未訂閱
請問inherited後面加不加method有什麼差別, 在看了help檔後,因為英文能力太差< >,還是看不懂 以下是我翻的內容< > The reserved word inherited plays a special role in implementing polymorphic behavior. 保留字"繼承"扮演一個特殊的角色在執行多型的行為。 It can occur in method definitions, with or without an identifier after it. 他可以發生在一個方法的定義, 隨著或不隨著一個定義在他之後。 If inherited is followed by a method identifier, 如果"繼承"被跟隨一個方法的定義 it represents a normal method call, 它描述一個一般的方法被呼叫 except that the search for the method begins with the immediate ancestor of the enclosing method class? 除了找尋開始的方法隨著目前主先的關閉方法類別 For example, when inherited Create(...); occurs in the definition of a method, 發生在訂義一個方法 it calls the inherited Create. 呼叫繼承的建立 When inherited has no identifier after it, 當繼承之後沒有方法定義在他之後 it refers to the inherited method with the same name as the enclosing method. 他會參考相同名稱的法方作為關閉方法。 In this case, 在這個列子中 inherited can appear with or without parameters; 繼承可以顯示參數或沒有參數, if no parameters are specified, 如果沒有參數被描述 it passes to the inherited method the same parameters with which the enclosing method was called. 通過它繼承的方法相同參數隨其其關閉方法被呼叫 For example, inherited; occurs frequently in the implementation of constructors. It calls the inherited constructor with the same parameters that were passed to the descendant. 請問正確的意思是什麼啊.....@_@||| ---------------- 初出芧房程設師, 左鍵右鼠寫程式, 日扣夜寫眼框溼, 望能早成系分師。 ----------------
------
----------------
初出芧房程設師,
左鍵右鼠寫程式,
日扣夜寫眼框溼,
望能早成系分師。
----------------
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-04-23 11:21:45 IP:147.8.xxx.xxx 未訂閱
In other words, if the compiler can work out the 'method' for you, you can omit the method after inherited., e.g.
TObj1 = class
public
    constructor Create;
end;    constructor TObj1.Create;
begin
    inherited; // = inherited Create;
end;    TObj2 = class
public
    constructor Create(Int: integer);
end;    constructor TObj2.Create;
begin
    inherited Create; // CANNOT omit Create since different argument
end;
But for function returning, I think you MUST specify the name of the function, e.g.
Result := inherited MyFunction;
Finally there is no need/way to specify the 'method' in using inherited in a message handler, e.g. from delphi help:
TTextBox = class(TCustomControl)
  private
    procedure WMChar(var Message: TWMChar); message WM_CHAR;
    ...
  end;    procedure TTextBox.Refresh(var Message: TMessageRecordType);
begin
  if Chr(Message.Code) = #13 then
    ...
  else
    inherited; // call the previous handler
end;
skurama
中階會員


發表:88
回覆:127
積分:73
註冊:2002-07-22

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-04-23 13:48:40 IP:61.219.xxx.xxx 未訂閱
請問你的意思是否是指: 當今天 public constructor Create; end; constructor TObj1.Create; begin inherited; // = inherited Create; end; 的時候,則 inherited 等於 inherited 在constructor Create(Int: integer); 則需要加上 inherited create; 是這樣嗎? 因為我看到一個元件的元始碼中 他寫建構者時 public constructor Create(AOwner: TComponent); override; 在程式中 constructor TTestComponent.Create(AOwner: TComponent); begin inherited Create(AOwner); End; 有加上Create(AOwner); 可是移除掉Create(AOwner)程式仍然可以過 至於CANNOT omit Create since different argument 是否指不能忽略 Create這個method來自不同的參數? 那為什麼建立元件時,下達inherited不加Create(AOwner)仍然可以完成。 ---------------- 初出芧房程設師, 左鍵右鼠寫程式, 日扣夜寫眼框溼, 望能早成系分師。 ----------------
------
----------------
初出芧房程設師,
左鍵右鼠寫程式,
日扣夜寫眼框溼,
望能早成系分師。
----------------
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-04-23 14:10:06 IP:147.8.xxx.xxx 未訂閱
引言: 請問你的意思是否是指: 當今天 public constructor Create; end; constructor TObj1.Create; begin inherited; // = inherited Create; end; 的時候,則 inherited 等於 inherited 在constructor Create(Int: integer); 則需要加上 inherited create; 是這樣嗎? Yes. because the parameter is different from the base class. 因為我看到一個元件的元始碼中 他寫建構者時 public constructor Create(AOwner: TComponent); override; 在程式中 constructor TTestComponent.Create(AOwner: TComponent); begin inherited Create(AOwner); End; 有加上Create(AOwner); 可是移除掉Create(AOwner)程式仍然可以過 至於CANNOT omit Create since different argument 是否指不能忽略 Create這個method來自不同的參數? 那為什麼建立元件時,下達inherited不加Create(AOwner)仍然可以完成。 Becasue the constructor of one of the base class (TComponent) is like: constructor Create(AOwner: TObject); So the method name and the parameter can be omitted. BTW, there is no harm (except a few more bytes for the source) in specifying the method name and parameter. ---------------- 初出芧房程設師, 左鍵右鼠寫程式, 日扣夜寫眼框溼, 望能早成系分師。 ----------------
danny
版主


發表:100
回覆:522
積分:595
註冊:2002-03-11

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-04-23 14:30:55 IP:211.76.xxx.xxx 未訂閱
引言: 請問inherited後面加不加method有什麼差別, 在看了help檔後,因為英文能力太差< >,還是看不懂 以下是我翻的內容< > The reserved word inherited plays a special role in implementing polymorphic behavior. 保留字"繼承"扮演一個特殊的角色在執行多型的行為。 It can occur in method definitions, with or without an identifier after it. 他可以發生在一個方法的定義, 隨著或不隨著一個定義在他之後。 If inherited is followed by a method identifier, 如果"繼承"被跟隨一個方法的定義 it represents a normal method call, 它描述一個一般的方法被呼叫 except that the search for the method begins with the immediate ancestor of the enclosing method class? 除了找尋開始的方法隨著目前主先的關閉方法類別 For example, when inherited Create(...); occurs in the definition of a method, 發生在訂義一個方法 it calls the inherited Create. 呼叫繼承的建立 When inherited has no identifier after it, 當繼承之後沒有方法定義在他之後 it refers to the inherited method with the same name as the enclosing method. 他會參考相同名稱的法方作為關閉方法。 In this case, 在這個列子中 inherited can appear with or without parameters; 繼承可以顯示參數或沒有參數, if no parameters are specified, 如果沒有參數被描述 it passes to the inherited method the same parameters with which the enclosing method was called. 通過它繼承的方法相同參數隨其其關閉方法被呼叫 For example, inherited; occurs frequently in the implementation of constructors. It calls the inherited constructor with the same parameters that were passed to the descendant. 請問正確的意思是什麼啊.....@_@|||
您翻的差不多了啊! 如果 inherited 後面沒有接任何 "參數" (這個說法有點怪, 不過您瞭解我的意思吧), 指的是此父階同樣的 method. 如: 在 Create method 中, inherited Create(AOnwer); 和 inherited; 效果相同, 但是依 VCL 的設計慣例是要寫的. 但是 inherited 後面既然可以加上 "參數", 就有很多的變化了, 例如: Create 中呼叫 inherited Destroy(這個例子當然沒有意義) inherited MyMethod; inherited FMyComponent.MyMethod; 都是可以的.
------
將問題盡快結案也是一種禮貌!
系統時間:2024-04-29 21:38:30
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!