가시성[접근자] Objc
2009.11.20 17:13 Edit
@interface A : Object
{
//objc의 기본선언은 protected다.
int z; //protected
@public
int a; //public
@protected
int b; //protected
@private
int c; //private
int d; //private
}
@end
{
//objc의 기본선언은 protected다.
int z; //protected
@public
int a; //public
@protected
int b; //protected
@private
int c; //private
int d; //private
}
@end
접근 테스트
#import <stdio.h>
#import <objc/Object.h>
@interface A : Object
{
@public
int a;
@protected
int b;
@private
int c;
}
- (id)initWithA:(int)a int:(int)b int:(int)c;
- (void)WriteA;
@end
@interface B : A
- (void)WriteB;
@end
@implementation A
- (id)initWithA:(int)a int:(int)b int:(int)c {
self->a = a;
self->b = b;
self->c = c;
return self;
}
- (void)WriteA {
printf("[A Write Method, a=%d, b=%d, c=%d]\n", a , b , c);
}
@end
@implementation B
- (void)WriteB {
printf("[B Write Method, a=%d, b=%d]\n", a , b);
}
@end
int main() {
B * objb = [[B new] initWithA:1000 int:100 int:10];
printf("[main() scope, a=%d]\n" , objb->a);
[objb WriteB];
[objb WriteA];
[objb free];
return 0;
}
#import <objc/Object.h>
@interface A : Object
{
@public
int a;
@protected
int b;
@private
int c;
}
- (id)initWithA:(int)a int:(int)b int:(int)c;
- (void)WriteA;
@end
@interface B : A
- (void)WriteB;
@end
@implementation A
- (id)initWithA:(int)a int:(int)b int:(int)c {
self->a = a;
self->b = b;
self->c = c;
return self;
}
- (void)WriteA {
printf("[A Write Method, a=%d, b=%d, c=%d]\n", a , b , c);
}
@end
@implementation B
- (void)WriteB {
printf("[B Write Method, a=%d, b=%d]\n", a , b);
}
@end
int main() {
B * objb = [[B new] initWithA:1000 int:100 int:10];
printf("[main() scope, a=%d]\n" , objb->a);
[objb WriteB];
[objb WriteA];
[objb free];
return 0;
}
이 글과 관련된 글
- [2009/11/20] 클래스 오브젝트 (400)
- [2009/11/20] 클래스 메소드 (466)
- [2009/11/20] 정적 클래스 (412)
- [2009/11/20] 소멸자 [free] (331)
- [2009/11/20] 생성자 [constructor] (274)
- Tag :
- Objc , Obj-C , Objective-C
