在C++中,类方法有重载(overload)、覆盖(override)和隐藏(hide)的概念(当然,函数也可重载。)。 C++的覆盖和隐藏是个非常容易混淆的概念,尤其隐藏。学到Objective-C,因为它与C++有相似的地方,所以对此也格外留心。 从下面代码测试结果可以看出,OC远没有C++那么“灵活”。具体结论输出信息一目了然。 /** * test_Hide.m */ #import <Foundation/Foundation.h> // ---- @interface section ---- // Base @interface Base: NSObject /* {} */ // It's optional. + (void) foo; + (void) foo: (int)param; + (void) foo: (int)param and: (int)param2; + (void) foo: (int)param and: (int)param2 and: (int)param3; - (void) bar; - (void) bar: (int)param; - (void) bar: (int)param and: (int)param2; - (void) bar: (int)param and: (int)param2 and: (int)param3; @end // Derived @interface Derived: Base + (void) foo: (int)param; - (void) bar: (int)param and: (int)param2; @end // ---- program section ---- int main(int argc, char* argv[]) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; [Base foo]; [Base foo: 1]; [Base foo: 1 and: 2]; [Base foo: 1 and: 2 and: 3]; Base* base = [[Base alloc] init]; [base bar]; [base bar: 1]; [base bar: 1 and: 2]; [base bar: 1 and: 2 and: 3]; [base release]; NSLog(@"--------< 1 >--------"); [Derived foo]; // Ok, either! NOT hided. [Derived foo: 11]; // OK [Derived foo: 11 and: 22]; // Ok, either! NOT hided. [Derived foo: 11 and: 22 and: 33]; // Ok, either! NOT hided. Derived* derived = [[Derived alloc] init]; [derived bar]; // Ok, either! NOT hided. [derived bar: 11]; // Ok, either! NOT hided. [derived bar: 11 and: 22]; // OK [derived bar: 11 and: 22 and: 33]; // Ok, either! NOT hided. [derived release]; NSLog(@"--------< 2 >--------"); [pool drain]; return 0; } // ---- @implementation section ---- // Base @implementation Base/* : NSObject */ // It's optional. + (void) foo { NSLog(@"Base::foo"); } + (void) foo: (int)param { NSLog(@"Base::foo: %i", param); } + (void) foo: (int)param and: (int)param2 { NSLog(@"Base::foo: %i and: %i", param, param2); } + (void) foo: (int)param and: (int)param2 and: (int)param3 { NSLog(@"Base::foo: %i and: %i and: %i", param, param2, param3); } - (void) bar { NSLog(@"Base::bar"); } - (void) bar: (int)param { NSLog(@"Base::bar: %i", param); } - (void) bar: (int)param and: (int)param2 { NSLog(@"Base::bar: %i and: %i", param, param2); } - (void) bar: (int)param and: (int)param2 and: (int)param3 { NSLog(@"Base::bar: %i and: %i and: %i", param, param2, param3); } @end // Derived @implementation Derived + (void) foo: (int)param { NSLog(@"Derived::foo: %i", param); } - (void) bar: (int)param and: (int)param2 { NSLog(@"Derived::bar: %i and: %i", param, param2); } @end // Output 2010-12-24 03:31:20.642 test_Hide[3700] Base::foo 2010-12-24 03:31:20.658 test_Hide[3700] Base::foo: 1 2010-12-24 03:31:20.658 test_Hide[3700] Base::foo: 1 and: 2 2010-12-24 03:31:20.658 test_Hide[3700] Base::foo: 1 and: 2 and: 3 2010-12-24 03:31:20.673 test_Hide[3700] Base::bar 2010-12-24 03:31:20.673 test_Hide[3700] Base::bar: 1 2010-12-24 03:31:20.673 test_Hide[3700] Base::bar: 1 and: 2 2010-12-24 03:31:20.689 test_Hide[3700] Base::bar: 1 and: 2 and: 3 2010-12-24 03:31:20.689 test_Hide[3700] --------< 1 >-------- 2010-12-24 03:31:20.689 test_Hide[3700] Base::foo 2010-12-24 03:31:20.705 test_Hide[3700] Derived::foo: 11 2010-12-24 03:31:20.705 test_Hide[3700] Base::foo: 11 and: 22 2010-12-24 03:31:20.705 test_Hide[3700] Base::foo: 11 and: 22 and: 33 2010-12-24 03:31:20.720 test_Hide[3700] Base::bar 2010-12-24 03:31:20.720 test_Hide[3700] Base::bar: 11 2010-12-24 03:31:20.720 test_Hide[3700] Derived::bar: 11 and: 22 2010-12-24 03:31:20.736 test_Hide[3700] Base::bar: 11 and: 22 and: 33 2010-12-24 03:31:20.736 test_Hide[3700] --------< 2 >--------