정적 클래스 Objc
2009.11.20 17:12 Edit
#import <stdio.h>
#import <objc/Object.h>
@interface A : Object
- (void) Write;
@end
@implementation A
- (void) Write {
printf("I am the born of my sword.\n");
}
@end
int main() {
id obj1 = [A new];
id obj2 = [Object new];
[obj1 Write];
[obj2 Write]; //실행시 에러
return 0;
}
#import <objc/Object.h>
@interface A : Object
- (void) Write;
@end
@implementation A
- (void) Write {
printf("I am the born of my sword.\n");
}
@end
int main() {
id obj1 = [A new];
id obj2 = [Object new];
[obj1 Write];
[obj2 Write]; //실행시 에러
return 0;
}
컴파일시 되지만 실행시 에러가 되는 코드다.
Object Class인 obj2 는 Write라는 Method가 없기 때문에 실행시 에러다.
컴파일시 정적으로 클래스형을 판정시키려면 ,id 형을 사용하지 않고 클래스형의 포인터를 사용해 인스턴스를 참조한다.
다음은 클래스 인스턴스를 포인터형으로 인스턴스의 주소를 넘긴다.
#import <stdio.h>
#import <objc/Object.h>
@interface A : Object
- (void)Write;
@end
@implementation A
- (void)Write {
printf("A . Write Method\n");
}
@end
int main() {
A * obja = [A new]; //A 클래스의 인스턴스를 A * 형태의 변수 obja 에 할당
[obja Write];
[obja free];
return 0;
}
#import <objc/Object.h>
@interface A : Object
- (void)Write;
@end
@implementation A
- (void)Write {
printf("A . Write Method\n");
}
@end
int main() {
A * obja = [A new]; //A 클래스의 인스턴스를 A * 형태의 변수 obja 에 할당
[obja Write];
[obja free];
return 0;
}
이 글과 관련된 글
- [2009/11/20] 클래스 오브젝트 (400)
- [2009/11/20] 클래스 메소드 (466)
- [2009/11/20] 가시성[접근자] (311)
- [2009/11/20] 소멸자 [free] (331)
- [2009/11/20] 생성자 [constructor] (274)
- Tag :
- Objc , Obj-C , Objective-C
