Method overloading, overwriting, and hiding in objective-C

Source: Internet
Author: User

In C ++, class methods include overload, override, and hide (of course, functions can also be overloaded .).

The coverage and hiding of C ++ is a very confusing concept, especially hidden. Learn objective-C, because it is similar to C ++, so pay special attention to this.

From the followingCodeTest results show that OC is far less flexible than C ++ ". The output of specific conclusions is clear at a glance.

/** <Br/> * test_hide.m <br/> */</P> <p> # import <Foundation/Foundation. h> </P> <p> // ---- @ interface section ---- </P> <p> // base <br/> @ interface base: nsobject <br/>/* {} * // It's optional. <br/> + (void) Foo; <br/> + (void) FOO: (INT) Param; <br/> + (void) FOO: (INT) param and: (INT) param2; <br/> + (void) FOO: (INT) PARAM and: (INT) param2 and: (INT) param3; <br/>-(void) bar; <br/>-(void) bar: (INT) para M; <br/>-(void) bar: (INT) PARAM and: (INT) param2; <br/>-(void) bar: (INT) PARAM and: (INT) param2 and: (INT) param3; <br/> @ end </P> <p> // derived <br/> @ interface derived: base <br/> + (void) FOO: (INT) Param; <br/>-(void) bar: (INT) PARAM and: (INT) param2; <br/> @ end </P> <p> // ---- program section ---- </P> <p> int main (INT argc, char * argv []) <br/> {<br/> NSAID utoreleasepool * Pool = [NSAID utoreleasepool al Loc] init]; </P> <p> [base Foo]; <br/> [base FOO: 1]; <br/> [base FOO: 1 and: 2]; <br/> [base FOO: 1 and: 2 and: 3]; </P> <p> base * base = [[base alloc] init]; <br/> [base bar]; <br/> [base bar: 1]; <br/> [base bar: 1 and: 2]; <br/> [base bar: 1 and: 2 and: 3]; <br/> [base release]; </P> <p> nslog (@ "-------- <1> --------"); </P> <p> [derived Foo]; // OK, either! Not hided. <br/> [derived FOO: 11]; // OK <br/> [derived FOO: 11 and: 22]; // OK, either! Not hided. <br/> [derived FOO: 11 and: 22 and: 33]; // OK, either! Not hided. </P> <p> derived * derived = [[derived alloc] init]; <br/> [derived bar]; // OK, either! Not hided. <br/> [derived bar: 11]; // OK, either! Not hided. <br/> [derived bar: 11 and: 22]; // OK <br/> [derived bar: 11 and: 22 and: 33]; // OK, either! Not hided. <br/> [derived release]; </P> <p> nslog (@ "-------- <2> --------"); </P> <p> [pool drain]; <br/> return 0; <br/>}</P> <p> // ---- @ implementation section ---- </P> <p> // base <br/> @ implementation base /*: nsobject * // It's optional. <br/> + (void) Foo <br/> {<br/> nslog (@ "base: foo "); <br/>}</P> <p> + (void) FOO: (INT) Param <br/>{< br/> nslog (@ "base: FOO: % I ", Param); <br/>}</P> <p> + (void) FOO: (INT) PARAM and: (INT) param2 <br/>{< br/> nslog (@ "base: FOO: % I and: % I", Param, param2 ); <br/>}</P> <p> + (void) FOO: (INT) PARAM and: (INT) param2 and: (INT) param3 <br/>{< br/> nslog (@ "base: FOO: % I and: % I", Param, param2, param3 ); <br/>}</P> <p>-(void) bar <br/>{< br/> nslog (@ "base: bar "); <br/>}</P> <p>-(void) bar: (INT) Param <br/>{< br/> nslog (@ "base: bar: % I ", Param); <br/>}</P> <p>-(void) bar: (INT) PARAM and: (INT) param2 <br/>{< br/> nslog (@ "base: bar: % I and: % I", Param, param2 ); <br/>}</P> <p>-(void) bar: (INT) PARAM and: (INT) param2 and: (INT) param3 <br/>{< br/> nslog (@ "base: bar: % I and: % I", Param, param2, param3 ); <br/>}< br/> @ end </P> <p> // derived <br/> @ implementation derived <br/> + (void) FOO: (INT) Param <br/>{< br/> nslog (@ "derived: FOO: % I", Param ); <br/>}</P> <p>-(void) bar: (INT) PARAM and: (INT) param2 <br/>{< br/> nslog (@ "derived: bar: % I and: % I", Param, param2 ); <br/>}< br/> @ end <br/>

// Output </P> <p> 03:31:20. 642 test_hide [3700] Base: Foo <br/> 03:31:20. 658 test_hide [3700] Base: FOO: 1 <br/> 03:31:20. 658 test_hide [3700] Base: FOO: 1 and: 2 <br/> 03:31:20. 658 test_hide [3700] Base: FOO: 1 and: 2 and: 3 <br/> 03:31:20. 673 test_hide [3700] Base: bar <br/> 03:31:20. 673 test_hide [3700] Base: bar: 1 <br/> 03:31:20. 673 test_hide [3700] Base: bar: 1 and: 2 <br/> 03:31:20. 689 test_hide [3700] Base: bar: 1 and: 2 and: 3 <br/> 03:31:20. 689 test_hide [3700] -------- <1> -------- <br/> 03:31:20. 689 test_hide [3700] Base: Foo <br/> 03:31:20. 705 test_hide [3700] derived: FOO: 11 <br/> 03:31:20. 705 test_hide [3700] Base: FOO: 11 and: 22 <br/> 03:31:20. 705 test_hide [3700] Base: FOO: 11 and: 22 and: 33 <br/> 03:31:20. 720 test_hide [3700] Base: bar <br/> 03:31:20. 720 test_hide [3700] Base: bar: 11 <br/> 03:31:20. 720 test_hide [3700] derived: bar: 11 and: 22 <br/> 03:31:20. 736 test_hide [3700] Base: bar: 11 and: 22 and: 33 <br/> 03:31:20. 736 test_hide [3700] -------- <2> --------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.