부모 클래스가 있습니다.하위 클래스 함수에서 부모 클래스 인스턴스 변수에 액세스
헤더 파일 (Parent.h) :
@interface Parent
@end
구현 파일 (Parent.m) : 다음
@interface Parent {
// I defined a instance vaiable 'name'
NSString *name;
// another custom type instance variable
School *mySchool;
}
@end
@implementation Parent
...
@end
, 나는 이Parent
을 상속 Child
클래스가 있습니다.
헤더 (Child.h) :
@interface Child : Parent
-(void)doSomething;
@end
구현 파일 (Child.m) :
@implementation Child
-(void)doSomething{
// Here, how can I access the instance variable 'name' defined in Parent class?
// I mean how to use the 'name' instance, not only get its value.
// for example: call writeToFile:atomically:encoding:error: on 'name' here
// tried to access mySchool defined in parent class
// Property 'mySchool' not found on object of type 'Parent'
School *school = [self valueForKey:@"mySchool"];
}
@end
어떻게 자식 클래스 함수에서 상위 클래스에 정의 된 인스턴스 변수를 액세스 할 수 있습니까?
==== 대한 설명 ===
나는 '이름'인스턴스를 사용하는 방법을 의미, 그 값을 구할 수있을뿐만 아니고. 예 : : '이름'에 writeToFile:atomically:encoding:error:을 호출하십시오.
속성을 추가하려면'Parent + Private.h' 파일을 사용할 수 있습니다. 왜냐하면 y 다른 객체가'Parent' 객체의'name'에 접근하는 것을 원하지 않습니다. – Larme
왜 Parent.h에 변수를 정의하지 않습니까? 그러면'self-> name'을 사용하여 액세스 할 수 있습니다. – KudoCC
h 파일에 선언 된 경우 하위 클래스의 self.name을 사용하여 직접 액세스 할 수 있습니다. –