首页 > 其他 > 详细

Object-c基本语法

时间:2014-08-10 12:57:40      阅读:369      评论:0      收藏:0      [点我收藏+]

//

//  main.m

//  OCbasic1

//

//  Created by apple on 14-8-5.

//  Copyright (c) 2014年 苹果IOS软件开发者. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import "Dog.h"

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        // insert code here...

        NSLog(@"Hello, World!");

        //write code here.

        Dog *dog = [[Dog alloc] init];

        //创建一个dog对象并初始化

        

        //定义参数,调用方法

         int ID = [dog getID];

         int age = [dog getage];

        float price = [dog getprice];

        printf("dog ID: %d age: %d price: %f\n",ID,age,price);

        //dog ID: 1 age: 2 price: 60.000000

        

        Dog *dog2 = [[Dog alloc ]initWithID:20 andage:5 andprice:100.88];

        ID =[dog2 getID];

        age = [dog2 getage];

        price = [dog2 getprice];

        printf("dog2 ID: %d age: %d price: %f\n",ID,age,price);

        //dog2 ID: 20 age: 5 price: 100.879997    return 0;

        

        [dog2 setID:19 andage:4 andprice:86.8];

        ID =[dog2 getID];

        age = [dog2 getage];

        price = [dog2 getprice];

        printf("dog2 ID: %d age: %d price: %f\n",ID,age,price);    }

}

 

 

 

 

 

 

 

//

//  Dog.h

//  OCbasic1

//

//  Created by apple on 14-8-5.

//  Copyright (c) 2014年 苹果IOS软件开发者. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject

{

    //字段(成员变量)及其访问权限

@protected

    int ID;

@public

    int age;

@private

    float price;

}

//凡是以initXX开头的都是构造函数

- (id) init;

//函数名为init,不带参数

- (id) initWithID:(int)newID;

//函数名为initWith:,带一个int的参数

- (id) initWithID:(int)newID andage:(int)newage;

//函数名为initWithID:andage:,带两个int参数

- (id) initWithID:(int)newID andage:(int)newage andprice:(float)newprice;

//函数名为initWithID:andage:andprince

 

- (void) setID:(int)newID;

- (int) getID;

//set/get ID

 

- (void) setage:(int)newage;

- (int) getage;

//set/get age

 

- (void) setprice:(float)newprice;

- (float) getprice;

//set/get price

 

- (void) setID:(int)newID andage:(int)newage;

// setID:andage:两个参数

 

- (void) setID:(int)newID andage:(int)newage andprice:(float)newprice;

// setID:andage:andprice: 3个参数

 

 

 

 

 

 

@end

 

 

 

 

 

//

//  Dog.m

//  OCbasic1

//

//  Created by apple on 14-8-5.

//  Copyright (c) 2014年 苹果IOS软件开发者. All rights reserved.

//

 

#import "Dog.h"

 

@implementation Dog

 

- (id)init

{

    return [self initWithID:1 andage:2 andprice:60.0];

//    self = [super init];

//    // super表示父类

//    // self 表示对象自己

//    if (self)

//    {

//        ID = 1;

//        age = 2;

//        price = 60.0f;

//    }

//    return self;

    

}

 

- (id)initWithID:(int)newID

{

    return [self initWithID:newID andage:2];

//    self = [super init];

//    if (self) {

//        ID = newID;

//        age = 2;

//        price = 60.0f;

//    }

//    return self;

}

 

- (id)initWithID:(int)newID andage:(int)newage

{

    return [self initWithID:newID andage:newage andprice:60.0f];

//    self = [super init];

//    if (self) {

//        ID = newID;

//        age = newage;

//        price = 60.0f;

//    }

//    return self;

    

}

 

- (id)initWithID:(int)newID andage:(int)newage andprice:(float)newprice

{

    self = [super init];

    if (self) {

        ID = newID;

        age = newage;

        price = newprice;

    }

    return self;

}

 

- (void)setID:(int)newID

{

    ID = newID;

}

- (int)getID

{

    return ID;

}

 

- (void)setage:(int)newage

{

    age = newage;

}

- (int)getage

{

    return age;

}

 

- (void)setprice:(float)newprice

{

    price = newprice;

}

- (float)getprice

{

    return price;

}

 

- (void)setID:(int)newID andage:(int)newage

{

    ID = newID;

    age = newage;

    

}

 

- (void)setID:(int)newID andage:(int)newage andprice:(float)newprice

{

    ID = newID;

    age = newage;

    price = newprice;

}

 

@end

 

Object-c基本语法,布布扣,bubuko.com

Object-c基本语法

原文:http://www.cnblogs.com/z-j-w/p/3902650.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!