// main.m
// 冒泡排序法
//
// Created by on 14-3-15.
// Copyright (c) 2014年 com.csdn. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
int arr[10]={12,3,45,67,89,14,5,8,9,33}; //定义一个整形的数组
for (int i=0;i<10;i++){ //先输出排序之前的数组的顺序
printf("%d\t",arr[i]);
}
NSLog(@" ");
int temp=0; //定义临时变量
for (int i=0;i<10;i++){ //外层循环控制循环多少次
for(int j=0;j<10-i;j++){ //循环控制2个2个相比较
//判断相邻的元素,前一个元素是否大于后一个元素,如果大于则交换
if(arr[j]>arr[j+1]){
temp =arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
//先输出排序之前的数组的顺序
for (int i=0;i<10;i++){
printf("%d\t",arr[i]);
}
}
return 0;
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
如何在.h和.m里面声明和实现。最后在main.m里面调用。
例子:在一个时间类里面,声明并调用函数,时分秒。在主函数里面打印出来。
Time.h的内容
//
// Time.h
// 面向对象1
//
// Created by on 14-3-15.
// Copyright (c) 2014年 com.csdn. All rights reserved.
//
//引入头文件
#import <Foundation/Foundation.h>
//如何去定义一个类
@interface MyTime : NSObject{
int miao;
int fen;
int shi;
}
-(void)setMyTime:(int)_fen;
-(int)getMyfen;
-(void)setMyTime:(int)_shi andfen:(int)_fen andmiao:(int)_miao;
-(void)getMyTime;
@end
Time.m里的内容
//
// Time.m
// 面向对象1
//
// Created by on 14-3-15.
// Copyright (c) 2014年 com.csdn. All rights reserved.
//
#import "Time.h" //实现头文件中定义的方法
@implementation MyTime
-(void)setMyTime:(int)_fen{
fen=_fen;
NSLog(@"setMyTime已调用");
}
-(int)getMyfen{
NSLog(@"getMyTime已调用");
return fen;
}
-(void)setMyTime:(int)_shi andfen:(int)_fen andmiao:(int)_miao{
shi=_shi;
fen=_fen;
miao=_miao;
}
-(void)getMyTime{
NSLog(@"时间为:%d时%d分%d秒",shi,fen,miao);
}
@end
主函数中的东西
//
// main.m
// 面向对象1
//
// Created by on 14-3-15.
// Copyright (c) 2014年 com.csdn. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Time.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//实例化对象
MyTime * time = [[MyTime alloc] init];
[time setMyTime:16];
int a = [time getMyfen];
NSLog(@"%d",a);
MyTime * time2= [[MyTime alloc] init];
[time2 setMyTime:14 andfen:54 andmiao:23];
[time2 getMyTime];
}
return 0;
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
自动生成变量访问
Person.h里面的内容
//
// Person.h
// 自动生成变量访问
//
// Created by 谷建鹏 on 14-3-16.
// Copyright (c) 2014年 com.csdn. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person :NSObject
{
NSString *name;
int age;
char sex;
int pid;
int tel;
}
@property int age,pid,tel; //相当于 -(void)setAge:(int) _age;
@property NSString *name;
@property char sex;
@end
Person.m里面的内容
//
// Person.m
// 自动生成变量访问
//
// Created by 谷建鹏 on 14-3-16.
// Copyright (c) 2014年 com.csdn. All rights reserved.
//
#import "Person.h"
@implementation Person
@synthesize age,pid,tel,name,sex;
@end
主函数里面的内容
//
// Person.h
// 自动生成变量访问
//
// Created by 谷建鹏 on 14-3-16.
// Copyright (c) 2014年 com.csdn. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person *person =[[Person alloc] init];
//设定name属性
[person setName:@"张三"];
[person setAge:56];
[person setSex:'F'];
NSLog(@"name = %@,age = %d,sex = %c",[person name],[person age],[person sex]);
}
return 0;
}