【学习笔记之ios开发】1.Object-C语法概述

Dog.m

//
//  Dog.m
//  OCBasic1
//
//  Created by peter on 14-1-26.
//  Copyright (c) 2014年 peter. All rights reserved.
//

#import "Dog.h"

@implementation Dog

- (id) init
{
    return [self initWithID:1];
//    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 andPrice:60.0f];
//    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];
}


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

Dog.h

//  OCBasic1
//  Dog.m
//  Created by peter on 14-1-26.
//  Copyright (c) 2014年 peter. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Dog : NSObject

{   //写字段
@protected
    int ID;
@public
    int age;
@private
    float price;
}
//凡是以initXXXX开头的都是构造函数
- (id) init;
//函数名为init   不带参数
- (id) initWithID:(int)newID;
//函数名为initWithID:带一个int的参数
- (id) initWithID:(int)newID andAge:(int)newAge;
//函数名为initWithID:andAge:带两个参数,都为int
- (id) initWithID:(int)newID andAge:(int)newAge andPrice:(float)newPrice;
//函数名为initWithID:andAge:andPrice:带3个参数


- (void) setID:(int)newID;
- (int) getID;
//set/get ID
- (void) setAge:(int)newAge;
- (int) getAge;

- (void) setPrice:(float)newPrice;
- (float) getPrice;

- (void) setID:(int)newID andAge:(int)newAge;
//setID:andAge: 两个参数
- (void) setID:(int)newID andAge:(int)newAge andPrice:(float)newPrice;
//setID:andAge:andPrice  三个参数



@end

main.m

//
//  main.m
//  OCBasic1
//
//  Created by peter on 14-1-26.
//  Copyright (c) 2014年 peter. 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 *dog1 = [Dog alloc];
        [dog1 init];
        
        int ID = [dog1 getID];
        int age = [dog1 getAge];
        float price = [dog1 getPrice];
        
        printf("dog1 id is %d age is %d price is %f\n",ID,age,price);
        //dog1 id is 1 age is 2 price is 60.000000
        
//        Dog *dog2 = [Dog alloc];
//        [dog2 initWithID:100 andAge:26 andPrice:68.88];
        Dog *dog2 = [[Dog alloc] initWithID:100 andAge:36 andPrice:68.88];
        ID = [dog2 getID];
        age = [dog2 getAge];
        price = [dog2 getPrice];
        printf("dog2 id is %d age is %d price is %f\n",ID,age,price);
        //dog2 id is 100 age is 36 price is 68.879997
        
        [dog2 setID:2014 andAge:38 andPrice:87.2];
        ID = [dog2 getID];
        age = [dog2 getAge];
        price = [dog2 getPrice];
        printf("dog2 new id is %d age is %d price is %f\n",ID,age,price);
        //dog2 new id is 2014 age is 38 price is 87.199997
    }
    return 0;
}

工程源码链接:

http://pan.baidu.com/s/1nt4p4ut


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值