Tonight I was learning about subclass and superclass. I have a question about accessing methods from MAIN. In ClassA I created an INT called X and created a method called -(void) setNum. In ClassB I created an INT named Y but gave the method the identical name -(void) setNum as I did in ClassA. In MAIN I say [myB print]; and it will display the value of y which is 10 from ClassB. Is there a way to have it print the X value of 7 from ClassA if they both share the same method name? Or is this bad coding to have identical method names from different classes?
Code:
#import <Foundation/Foundation.h>
@interface ClassA : NSObject
{
int x;
}
-(void) setNum;
-(void) print;
@end
Code:
#import "ClassA.h"
@implementation ClassA
-(void) setNum
{
x = 7;
}
-(void) print
{
NSLog(@"It is %i", x);
}
@end
Code:
#import "ClassA.h"
@interface ClassB : ClassA
{
int y;
}
-(void) setNum;
-(void) print;
@end
Code:
#import "ClassB.h"
@implementation ClassB
-(void) setNum
{
y = 10;
}
-(void) print
{
NSLog(@"It is %i", y);
}
@end
Code:
#import "ClassB.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ClassB * myB = [[ClassB alloc] init];
[myB setNum];
[myB print];
[myB release];
[pool drain];
return 0;
}