[問題] Category自動導入?
大家好,我有一個初階的問題。
我有一個Fraction類別
我在類別裡定義一個函式 -(BOOL) isEqualTo: (id) object
我另外有幫Fraction寫了一個目錄Comparison
裡面也有定義一個函式 -(BOOL) isEqualTo: (Fraction *) f
我在main.m裡面沒有導入Fraction+Comparison.h
但我在呼叫isEqualTo:的時候 它卻會去跑Comparison裡的函式
而且有跑出警告 似乎是說Comparison的函式把Fraction的override掉了
但是我沒有導入這個目錄 我試過呼叫目錄裡的其他函式無法執行
還有這兩個函式不是不一樣嗎?怎麼會override?
這個是我的原始檔 https://db.tt/pVConWpC
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.76.64.68
→
01/02 08:55, , 1F
01/02 08:55, 1F
→
01/02 08:56, , 2F
01/02 08:56, 2F
→
01/02 08:57, , 3F
01/02 08:57, 3F
→
01/02 08:58, , 4F
01/02 08:58, 4F
所以意思是如果我用的函式如果category裡面也有定義
我有reference該category 但不管有沒有import
它都會去跑category的那個函式嗎?
而且一個引數類型是Fraction指標 一個是id
引數名稱也不同 還是會被覆寫?
→
01/02 12:58, , 5F
01/02 12:58, 5F
書上是說一般好像要
method name, return type, argument number, argument return types
都一樣,才有override發生
所以只要method name一樣就會被override嗎?
還是category的情況比較特殊?
→
01/03 11:00, , 6F
01/03 11:00, 6F
Programming in Objective-C, 5th, page 171
You can define a new method with the same name to replace the
inherited method... A method defined with the same name as that
of a parent class replaces, or overrides, the inherited definition.
Your new method must have the same return type and take the same
number and type of arguments as the method you are overriding.
所以它後面這句是錯的嗎?
→
01/03 19:16, , 7F
01/03 19:16, 7F
→
01/03 19:24, , 8F
01/03 19:24, 8F
→
01/03 19:26, , 9F
01/03 19:26, 9F
→
01/03 19:28, , 10F
01/03 19:28, 10F
嗯 這段是在繼承的章節裡提到
因為讀到category的時候不知道為什麼
沒有import的category裡的函式會自己跑
而且override掉main implementation裡的函式
兩個函式也沒有像書上說的完全一樣
不過我查了資料似乎它不會管類型
板友也說它不會管引數名字
和書上說的好像不同
※ 編輯: ishuen 來自: 211.76.64.68 (01/03 19:40)