S.h
#import<Foundation/Foundation.h>/** Solution: 1. Do not @public decoration 2. We object has two operations to access and set member variables 1> set Value p->age = ten; 2> Access value NSLog (@ "%d", p->age); Now there are two ways to set the value and access the value 1> the format of the member variable Convention:-(void) Set member variable name (minus underscore, first capitalization):(member variable data type) member variable name (remove underscore); -(void) Setage: (int) age; 2> the format of the Access member variable Convention:-(member variable data type) member variable name (minus underscore); -(int) age; *///. h files are exposed outside,. m files are hidden. @interfacestudent:nsobject{int_age;//age, the member variable is underlined, and the local variable is not underlined. NSString *_name;//name}//provide age-setting and access Methods- (void) Setage: (int) age;- (int) age;//methods for setting and accessing names- (void) SetName: (NSString *) name;-(NSString *) name;@end
S.m
#import "Student.h"@implementationStudent//provide age-setting and access Methods- (void) Setage: (int) age{if(Age >0&& Age < -) {_age=Age ; }Else{//Age =;_age = -; }}- (int) age{return_age;}//methods for setting and accessing names//compares whether two strings are the same-(BOOL) isequaltostring: (NSString *) astring;- (void) SetName: (NSString *) name{if([name length] >2) { if([Name isequaltostring:@"D Major"] || [Name isequaltostring:@"method in D major"]) {_name=@"Anonymous"; }Else{_name=name; } } Else{NSLog (@"Please re-enter!"); } }-(NSString *) name{return_name;}@end
Oc-16-set,get method