Student.h:
#import <Foundation/Foundation.h>
@interface Student : NSObject{
// @public
// @private
// @protected
//y一般默认的情况是@protected
int age;
@private
int no;
@public
float height;
}
//-(void) age;
@property(nonatomic,assign) int age;
@end
Student.m:
#import "Student.h"
@implementation Student
@end
GoodStudent.h:
#import "Student.h"
@interface GoodStudent : Student
@end
GoodStudent.m:
#import "GoodStudent.h"
@implementation GoodStudent
-(void) test{
age=10;
//no=9;
height=0.1f;
}
@end
main:
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Student *stu=[[[Student alloc] init] autorelease];
//点语法不是访问成员变量,是访问get和set方法
stu.age=10;
//通过get方法调用成员变量
NSLog(@"age is %i",stu.age);
//这样才是访问@public成员变量
stu->height=10.9f;
NSLog(@"height is %.1f",stu->height);
}
return 0;
}
结果:
2013-08-02 15:12:25.051 成员变量[988:303] age is 10
2013-08-02 15:12:25.072 成员变量[988:303] height is 10.9