OBJ-C Programming 04: Inheritance of Classes

Source: Internet
Author: User

This 4th article is less, the main thing is the inheritance of the class in the obj-c, it is necessary to explain that I just wrote the simplest form of inheritance, assuming that all unfold, that is more! The key is now the belly is not enough ink, can not unfold Ah!

In the following code, we wrote 2 classes: The parent Class A and subclass B, and then the methods in a in B are rewritten.

#import <Foundation/Foundation.h> @interface a:nsobject{int i;} @property int i;-(void) print, @end @implementation a@synthesize i;-(void) Print{nslog (@ "[Class a][i:%d]:hello!", I);} @end @interface B:a{int J;} @property Int J; @end @implementation b@synthesize j;-(void) Print{nslog (@ "[Class b:a][i:%d,j:%d]hello!", I,j);} @endint Main (int Argc,char *argv[]) {@autoreleasepool {NSLog (@ "Hello obj-c!"); a *a = [[a alloc] init]; b *b = [[b alloc] init];a.i = 101;B.I = 1001;B.J = 1002; [a print]; [b print];} return 0;}

the results of the compilation execution are as follows:

[Email protected]: objc_src$clang-fobjc-arc-framework Foundation 1.m-o 1

[Email protected]: OBJC_SRC$./1

2014-06-29 10:34:30.547 1[1035:507] Hello obj-c!

2014-06-29 10:34:30.549 1[1035:507] [class a][i:101]:hello!

2014-06-29 10:34:30.549 1[1035:507] [class b:a][i:1001,j:1002]hello!

OK, let's change the definition of instance variable I in Class A a little bit, make it private, that is, not defined in @interface and define I in @implementation. When compiling, you will find that print in Class B will error:


[Email protected]: objc_src$clang-fobjc-arc-framework Foundation 1.m-o 1

1.M:34:41: Error: instance variable ' i ' is private

NSLog (@ "[Class b:a][i:%d,j:%d]hello!", I,j);

Correction is also very easy, direct self.i can be, the following is the complete code after the change:

#import <Foundation/Foundation.h> @interface a:nsobject@property int i;-(void) set_i: (int) i;-(void) print;@ End@implementation a{int i;} @synthesize i;-(void) set_i: (int) i_v{i = I_v;} -(void) Print{nslog (@ "[Class a][i:%d]:hello!", I);} @end @interface B:a{int J;} @property Int J; @end @implementation b@synthesize j;-(void) Print{nslog (@ "[Class b:a][i:%d,j:%d]hello!", Self.i,j);} @endint Main (int Argc,char *argv[]) {@autoreleasepool {NSLog (@ "Hello obj-c!"); a *a = [[a alloc] init]; b *b = [[b alloc] init];//a.i = 101;//B.I = 1001; [A set_i:99]; [B SET_I:199];B.J = 1002; [a print]; [b print];} return 0;}

[Add content for the first time 2014-06-30: Visibility of instance variables]:

We talked about the syntax of defining class instance variables, in fact, the visible range of instance variables can be further refined control, there are 4 kinds of visibility, the default is the 1th type:

1 @protected: Instance variables can be directly interviewed by the class and subclass (default);

2 @private: Instance variables can only be interviewed by such methods;

3 @public: Instance variables can be visited by this class, subclass, and external methods (need to use the syntax, see the following code);

4 @package: In a 64-bit image, instance variables can be interviewed anywhere in the image. (Not very clear, OH)

Here's the code:

#import <Foundation/Foundation.h> @interface a:nsobject{@privateint pri_i; @protectedint i; @publicint pub_i;} @property int i,pri_i;-(void) set_i: (int) i;-(void) print; @end @implementation a@synthesize i=i,pri_i=pri_i;-(void) Set _i: (int) i_v{i = I_v;} -(void) Print{nslog (@ "[Class a][i:%d,pri_i:%d,pub_i:%d]:hello!", i,pri_i,pub_i);} @end @interface B:a{int J;} @property Int J; @end @implementation b@synthesize j;-(void) Print{//nslog (@ "[Class B:a]pri_i:%d", pri_i); Err!pri_i is privated! NSLog (@ "[Class b:a][i:%d,j:%d]hello!", I,j);} @endint Main (int Argc,char *argv[]) {@autoreleasepool {NSLog (@ "Hello obj-c!"); a *a = [[a alloc] init]; b *b = [[b alloc] init];a->pub_i = 10;b->pub_i = 20;//A.I = 101;//B.I = 1001; [A set_i:99]; [B SET_I:199];B.J = 1002; [a print]; [b print];} return 0;}




OBJ-C Programming 04: Inheritance of Classes

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.