Object-c基本文法

來源:互聯網
上載者:User

標籤:os   io   2014   ar   new   log   res   ios   

//

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.