Sian 发表于 2013-12-16 21:39:25

第五讲:练习(基本语法)

Dog.h
#import <Foundation/Foundation.h>
@interface Dos:NSObject
{
@protected
   int ID;
@public
   int age;
@private
   float price
}
- (id) init;
- (id) intWithID:(int)newID;
- (id) intWithID:(int)newID andAge:(int)newAge;
- (id) intWithID:(int)newID andAge:(int)newAge andPrice(float)newPrice;
- (void) setID:(int)newID;
- (int) getID;
- (void) setAge:(int)newAge;
- (int) getAge;
- (void) setPrice:(float)newPrice;
- (float) getPrice;
- (void)setID:(int)newID andAge:(int)newAge;
- (void)setID:(int)newID andAge:(int)newAge andPrice:(float)newPrice;
@end

Dog.m
#import "Dog.h"
@implenentation Dog
- (id) init
{
   self = ;
    if(self){
    ID = 1;
    age = 2;
    price = 60.0f;
    }
    return self;
    //return ;
}
- (id) initWithID:(int)newID
{
    self = ;
      if(self){
      ID = newID;
      age = 2;
      price = 60.0f;
      }
return self;
//return ;
}
- (id) initWithID:(int)newID andAge:(int)newAge
{
   self = ;
      if(self){
      ID = newID;
      age = newAge;
      price = 60.0f;
      }
   return self;
//return ;
}
- (id) initWithID:(int)newID andAge:(int)newAge andPrice(float)newPrice
{
   self = ;
      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

main.m
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[])
{
@autoreleasepool{
      Dog *dog1 = ;
      ;
      int ID = ;
      int age = ;
      float price = ;
      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.0000

      Dog *dog2 = [ initWithID:100 andAge:36 andPrice:68.88];
      int ID = ;
      int age = ;
      float price = ;
      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

      Dog *dog2 = [ setID:2012 andAge:38 andPrice:87.2];
      int ID = ;
      int age = ;
      float price = ;
      printf("dog2 new id is %d age is %d price is %f \n",ID,age,price);
      //dog2 net id is 2012 age is 38 price is 87.19998
   }
   return 0
}
页: [1]
查看完整版本: 第五讲:练习(基本语法)