作者ishuen (卢小小)
看板MacDev
标题[问题] Category自动导入?
时间Thu Jan 2 08:49:59 2014
大家好,我有一个初阶的问题。
我有一个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
1F:→ uranusjr:这和 objc_msgSend 背後的实作有关, 总之 category 如果 01/02 08:55
2F:→ uranusjr:进行 method overriding 通常效果会是全域 01/02 08:56
3F:→ uranusjr:总之除非你有复写内建类别的特殊需求, 否则最好不要这样 01/02 08:57
4F:→ uranusjr:使用 category; 苹果的文件有明确 discourage 这件事情 01/02 08:58
所以意思是如果我用的函式如果category里面也有定义
我有reference该category 但不管有没有import
它都会去跑category的那个函式吗?
而且一个引数类型是Fraction指标 一个是id
引数名称也不同 还是会被覆写?
5F:→ darktt:它的判断依据是Method名称,它是不管引数的 01/02 12:58
书上是说一般好像要
method name, return type, argument number, argument return types
都一样,才有override发生
所以只要method name一样就会被override吗?
还是category的情况比较特殊?
6F:→ likesea:你的书是哪一本?我的书不是这样写的.......... 01/03 11:00
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.
所以它後面这句是错的吗?
7F:→ howdiun:因为你用id所以会覆写 01/03 19:16
8F:→ howdiun:对不起我错了OTZ 01/03 19:24
9F:→ howdiun:书中讲的应该不是指category 01/03 19:26
10F:→ howdiun:inherited是指继承 01/03 19:28
嗯 这段是在继承的章节里提到
因为读到category的时候不知道为什麽
没有import的category里的函式会自己跑
而且override掉main implementation里的函式
两个函式也没有像书上说的完全一样
不过我查了资料似乎它不会管类型
板友也说它不会管引数名字
和书上说的好像不同
※ 编辑: ishuen 来自: 211.76.64.68 (01/03 19:40)