OC 的冒泡排序问题

//
//  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;
}
























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值