OC学习-2
第一个OC程序
学习oc写法和规范
#import <Foundation/Foundation.h> @interface Student : NSObject { int age; } // /* Set方法 1. 作用设置一个外部变量 2. 命名规范: 1》方法名以set开头 2》set后跟上变量名称,必须首字母大写 3》返回值一定是void 4》一定要接受一个参数并且与变量类型一直 5》行参的名字不能和成员变量一样 */ - (void)setAge:(int)newage; - (void)study; - (int)getAge; @end @implementation Student - (void)setAge:(int)newage{ if(newage < 1){ newage = 1; } age = newage; } - (int)getAge{ return age; } - (void)study{ NSLog(@"I can Study, my age is %d", age); } @end int main(){ Student *stu = [Student new]; [stu setAge:10]; [stu study]; NSLog(@"get aget:%d", [stu getAge]); }

浙公网安备 33010602011771号