ObjectiveC实例程序详细注释.docx
- 文档编号:24331207
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:183
- 大小:55.13KB
ObjectiveC实例程序详细注释.docx
《ObjectiveC实例程序详细注释.docx》由会员分享,可在线阅读,更多相关《ObjectiveC实例程序详细注释.docx(183页珍藏版)》请在冰豆网上搜索。
ObjectiveC实例程序详细注释
//main.m
//day01_01
#import
//子函数
voidprint(NSString*str)//str是形参
{
NSLog(@"%@",str);
}
intmain(intargc,constchar*argv[])
{
@autoreleasepool//自动释放池
{
//insertcodehere...
NSLog(@"Hello,World!
");//@是类的对象
intx=10;
doublepi=3.14;
NSLog(@"%d,%g",x,pi);
char*str="ThisisaCstring.";
NSLog(@"%s",str);
NSString*str1=@"ThisisaOCstring.";
NSLog(@"%@",str1);
print(str1);//str1是实参
}
return0;
}
//main.m
//day01_02
#import
intmain(intargc,constchar*argv[])
{
@autoreleasepool
{
boolz;
z=true;
z=false;
z=12344;
NSLog(@"%d",z);
BOOLy;
NSLog(@"字节数%lu",sizeof(y));
y=YES;
y=NO;
y=12345;//不要随便将一个非零值赋值给OC的BOOL类型变量
NSLog(@"%d",y);
}
return0;
}
//main.m
//day01_03
#import
structExample
{
inti;
charch;
//全部变量
};//结构体没有函数
@interfaceTRMyFirstClass:
NSObject//@interface表示类的开始
{
int_i;//属性,成员变量。
类成员名前加下划线,
char_ch;
//局部变量---类的作用域在@interface和@end之间
}
-(void)method;//方法。
只能写函数的声明
-(void)setI:
(int)i;//i是形参名
-(int)getI;//取值
-(void)setCH:
(char)ch;
-(char)getCH;
@end//表示类的结束
@implementationTRMyFirstClass//TRMyFirstClass
-(void)method
{
NSLog(@"这是一个类中的函数");
}
-(void)setI:
(int)i
{
_i=i;
}
-(int)getI
{
return_i;
}
-(void)setCH:
(char)ch
{
_ch=ch;
}
-(char)getCH
{
return_ch;
}
@end//TRMyFirstClass
@interfaceTRDog:
NSObject//TRDog类
{
NSString*_name;
}
-(void)setName:
(NSString*)name;
-(NSString*)getname;
-(void)bark;
@end
@implementationTRDog//TRDog
-(void)setName:
(NSString*)name
{
_name=name;
}
-(NSString*)getname
{
return_name;
}
-(void)bark
{
NSLog(@"狗狗%@正在旺旺的叫",_name);
}
@end;//TRDog
intmain(intargc,constchar*argv[])
{
@autoreleasepool
{
structExamplea;//a是局部变量,结构体变量在栈区
a.i=10;
//p->_i=10;//不能直接使用类中的成员变量
TRMyFirstClass*b=[TRMyFirstClassalloc];//类的变量在堆上,类的对象
[bmethod];//函数调用
[bsetI:
10];//将10存入成员变量_i
intc=[bgetI];//将成员变量_i的值取出来存入变量c中
NSLog(@"%d",c);
[bsetCH:
'a'];
charch=[bgetCH];
NSLog(@"%c",ch);
TRDog*dog=[TRDogalloc];
[dogsetName:
@"旺财"];
[dogbark];
}
return0;
}
//main.m
//day01-04
#import
@interfaceTRCat:
NSObject
{
NSString*_name;
}
-(void)setName:
(NSString*)name;
-(NSString*)getname;//-(NSString*)name;
-(void)bark;
@end
@implementationTRCat
-(void)setName:
(NSString*)name
{
_name=name;
}
-(NSString*)getname//-(NSString*)name;
{
return_name;
}
-(void)bark
{
NSLog(@"猫猫%@说:
\"喵了个咪\"",_name);
}
@end
intmain(intargc,constchar*argv[])
{
@autoreleasepool
{
TRCat*cat=[TRCatalloc];
[catsetName:
@"小黄"];
[catbark];
}
return0;
}
//TRCat.h
//day01_05
#import
@interfaceTRCat:
NSObject
{
NSString*_name;
}
-(void)setName:
(NSString*)name;
-(NSString*)name;
-(void)bark;
@end
//
//TRCat.m
//day01_05
#import"TRCat.h"
@implementationTRCat
-(void)setName:
(NSString*)name
{
_name=name;
}
-(NSString*)name
{
return_name;
}
-(void)bark
{
NSLog(@"猫咪%@说:
\"喵了个咪\".",_name);
}
@end
#import
#import"TRCat.h"
intmain(intargc,constchar*argv[])
{
@autoreleasepool
{
TRCat*cat=[TRCatalloc];
[catsetName:
@"可可"];
[catbark];
}
return0;
}
//TRStudent.h
//day01_06
#import
@interfaceTRStudent:
NSObject
{
NSString*_name;
BOOL_gender;
int_age;
NSString*_address;
}
-(void)setName:
(NSString*)name;//赋值
-(void)setGender:
(BOOL)gender;//赋值
-(void)setAge:
(int)age;//赋值
-(void)setAddress:
(NSString*)address;//赋值
-(NSString*)name;//取值
-(BOOL)gender;//取值
-(int)getAge;//取值
-(NSString*)address;//取值
-(void)show;//输出
@end
#import"TRStudent.h"
@implementationTRStudent
-(void)setName:
(NSString*)name
{
_name=name;
}
-(NSString*)name
{
return_name;
}
-(void)setGender:
(BOOL)gender
{
_gender=gender;
}
-(BOOL)gender
{
return_gender;
}
-(void)setAge:
(int)age
{
_age=age;
}
-(int)getAge
{
return_age;
}
-(void)setAddress:
(NSString*)address
{
_address=address;
}
-(NSString*)address
{
return_address;
}
-(void)show
{
NSLog(@"姓名:
%@",_name);
NSLog(@"性别:
%@",_gender?
@"男":
@"女");
NSLog(@"年龄:
%d",_age);
NSLog(@"地址:
%@",_address);
}
@end
#import
#import"TRStudent.h"
intmain(intargc,constchar*argv[])
{
@autoreleasepool
{
TRStudent*stu=[TRStudentalloc];
[stusetName:
@"张三"];
[stusetGender:
YES];
[stusetAge:
20];
[stusetAddress:
@"江苏南京"];
[stushow];
}
return0;
}
//
//TRTeacher.h
//day01_07
#import
@interfaceTRTeacher:
NSObject
{
NSString*_name;
int_age;
BOOL_gender;
NSString*_course;
NSString*_address;
}
-(void)setName:
(NSString*)name;
-(NSString*)name;
-(void)setAge:
(int)age;
-(int)age;
-(void)setGender:
(BOOL)gender;
-(BOOL)gender;
-(void)setCourse:
(NSString*)course;
-(NSString*)course;
-(void)setAddress:
(NSString*)address;
-(NSString*)address;
-(void)show;
@end
#import"TRTeacher.h"
@implementationTRTeacher
-(void)setName:
(NSString*)name
{
_name=name;
}
-(void)setAge:
(int)age
{
_age=age;
}
-(void)setGender:
(BOOL)gender
{
_gender=gender;
}
-(void)setCourse:
(NSString*)course
{
_course=course;
}
-(void)setAddress:
(NSString*)address
{
_address=address;
}
-(NSString*)name
{
return_name;
}
-(int)age
{
return_age;
}
-(BOOL)gender
{
return_gender;
}
-(NSString*)course
{
return_course;
}
-(NSString*)address
{
return_address;
}
-(void)show
{
NSLog(@"姓名:
%@,年龄:
%d,性别:
%@,课程:
%@,地址:
%@",_name,_age,_gender?
@"男":
@"女",_course,_address);
}
@end
#import
#import"TRTeacher.h"
intmain(intargc,constchar*argv[])
{
@autoreleasepool
{
TRTeacher*teacher=[TRTeacheralloc];
[teachersetName:
@"张三"];
[teachersetAge:
20];
[teachersetGender:
YES];
[teachersetCourse:
@"OC语言"];
[teachersetAddress:
@"湖北武汉"];
[teachershow];
}
return0;
}
//TRTime.h
//day01_08
#import
@interfaceTRTime:
NSObject
{
int_hour;
int_min;
int_sec;
}
-(void)setHour:
(int)hour;
-(int)hour;
-(void)setMin:
(int)min;
-(int)min;
-(void)setSec:
(int)sec;
-(int)sec;
-(void)show;
-(long)seconds;
@end
#import"TRTime.h"
@implementationTRTime
-(void)setHour:
(int)hour
{
_hour=hour;
}
-(int)hour
{
return_hour;
}
-(void)setMin:
(int)min
{
_min=min;
}
-(int)min
{
return_min;
}
-(void)setSec:
(int)sec
{
_sec=sec;
}
-(int)sec
{
return_sec;
}
-(void)show
{
NSLog(@"%02d:
%02d:
%02d",_hour,_min,_sec);
}
-(long)seconds
{
return_hour*3600+_min*60+_sec;
}
@end
#import
#import"TRTime.h"
intmain(intargc,constchar*argv[]){
@autoreleasepool
{
TRTime*time=[TRTimealloc];
[timesetHour:
1];
[timesetMin:
11];
[timesetSec:
11];
[timeshow];
NSLog(@"今天已经过了%ld秒",[timeseconds]);
}
return0;
}
//TRStudent.h
//day01_10
#import
@interfaceTRStudent:
NSObject
{
NSString*_name;
BOOL_gender;
int_age;
NSString*_address;
int_score;
}
-(void)setName:
(NSString*)name;
-(NSString*)name;
-(void)setGender:
(BOOL)gender;
-(BOOL)gender;
-(void)setAge:
(int)age;
-(int)age;
-(void)setAddress:
(NSString*)address;
-(NSString*)address;
-(void)setScore:
(int)score;
-(void)show;
@end
#import"TRStudent.h"
@implementationTRStudent
-(void)setName:
(NSString*)name
{
_name=name;
}
-(NSString*)name
{
return_name;
}
-(void)setGender:
(BOOL)gender
{
_gender=gender;
}
-(BOOL)gender
{
return_gender;
}
-(void)setAge:
(int)age
{
_age=age;
}
-(int)age;
{
return_age;
}
-(void)setAddress:
(NSString*)address
{
_address=address;
}
-(NSString*)address
{
return_address;
}
-(void)setScore:
(int)score
{
_score=score;
}
-(int)score
{
return_score;
}
-(void)show
{
NSLog(@"姓名:
%@",_name);
NSLog(@"性别:
%@",_gender?
@"男":
@"女");
NSLog(@"年龄:
%d",_age);
NSLog(@"住址:
%@",_address);
NSLog(@"分数:
%d",_score);
}
@end
#import
#import"TRStudent.h"
intmain(intargc,constchar*argv[])
{
@autoreleasepool
{
TRStudent*stu=[TRStudentalloc];
[stusetName:
@"王五"];
[stusetGender:
YES];
[stusetAge:
23];
[stusetAddress:
@"湖北武汉"];
[stusetScore:
100];
[stushow];
}
return0;
}
//
//TRTeacher.h
//day01_11
#import
@interfaceTRTeacher:
NSObject
{
NSString*_name;
BOOL_gender;
int_age;
NSString*_course;
NSString*_address;
int_number;
char_rank;
}
-(void)setName:
(NSString*)name;
-(NSString*)name;
-(void)setGender:
(BOOL)gender;
-(BOOL)gender;
-(void)setAge:
(int)age;
-(int)age;
-(void)setCourse:
(NSString*)course;
-(NSString*)course;
-(void)setAddress:
(NSString*)address;
-(NSString*)address;
-(void)setNumber:
(int)number;
-(int)number;
-(void)setRank:
(char)rank;
-(char)rank;
-(void)show;
@end
#import"TRTeacher.h"
@implementationTRTeacher
-(void)setName:
(NSString*)name
{
_name=name;
}
-(NSString*)name
{
return_name;
}
-(void)setGender:
(BOOL)gender
{
_gender=gender;
}
-(BOOL)gender
{
return_gender;
}
-(void)setAge:
(int)age
{
_age=age;
}
-(int)age
{
return_age;
}
-(void)setCourse:
(NSString*)course
{
_course=course;
}
-(NSString*)course
{
return_course;
}
-(void)setAddress:
(NSString*)address
{
_address=address;
}
-(NSString*)address
{
return_address;
}
-(void)setNumber:
(int)number
{
_number=number;
}
-(int)number
{
return_number;
}
-(void)setRank:
(char)rank
{
_rank=rank;
}
-(char)rank
{
return_rank;
}
-(void)show
{
NSLog(@"姓名:
%@",_name);
NSLog(@"性别:
%@",_gender?
@"男":
@"女");
NSLog(@"年龄:
%d",_age);
NSLog(@"课程:
%@",_course);
NSLog(@"住址:
%@",_address);
NSLog
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ObjectiveC 实例 程序 详细 注释