新建一个学生类,包含属性age name 以及一些方法
Student.h:
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
int age;
NSString *name;
}
-(void)show;
-(void)setAge:(int)a;
//-(NSString*)name;
-(void)setName:(NSString*)n;
-(NSString*)getName;
@end
Student.m:
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
int age;
NSString *name;
}
-(void)show;
-(void)setAge:(int)a;
//-(NSString*)name;
-(void)setName:(NSString*)n;
-(NSString*)getName;
@end
main.m
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *info=@"hello world";
NSLog(@"%@",info);
Student* student=[[Student alloc] init];
[student setName:@"LiBaojie"];
[student setAge:24];
[student show];
student.name=@"李宝杰";
//NSLog(@"%@",student.name);
}
return 0;
}
如果释放name方法,则可以student.name。由此可见,name属性可以设置,但是不可以读取。