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

来源:互联网 发布:幼儿园课件制作软件 编辑:程序博客网 时间:2024/06/02 10:46

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
原创粉丝点击