年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2879|回复: 0

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

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2013-12-16 21:39:25 | 显示全部楼层 |阅读模式
    Dog.h
    1. #import <Foundation/Foundation.h>
    2. @interface Dos:NSObject
    3. {
    4. @protected
    5.      int ID;
    6. @public
    7.      int age;
    8. @private
    9.      float price
    10. }
    11. - (id) init;
    12. - (id) intWithID:(int)newID;
    13. - (id) intWithID:(int)newID andAge:(int)newAge;
    14. - (id) intWithID:(int)newID andAge:(int)newAge andPrice(float)newPrice;
    15. - (void) setID:(int)newID;
    16. - (int) getID;
    17. - (void) setAge:(int)newAge;
    18. - (int) getAge;
    19. - (void) setPrice:(float)newPrice;
    20. - (float) getPrice;
    21. - (void)setID:(int)newID andAge:(int)newAge;
    22. - (void)setID:(int)newID andAge:(int)newAge andPrice:(float)newPrice;
    23. @end
    复制代码

    Dog.m
    1. #import "Dog.h"
    2. @implenentation Dog
    3. - (id) init
    4. {
    5.    self = [super init];
    6.     if(self){
    7.     ID = 1;
    8.     age = 2;
    9.     price = 60.0f;
    10.     }
    11.     return self;
    12.     //return [self initWithID:1];
    13. }
    14. - (id) initWithID:(int)newID
    15. {
    16.     self = [super init];
    17.       if(self){
    18.       ID = newID;
    19.       age = 2;
    20.       price = 60.0f;
    21.       }
    22. return self;
    23. //return [self initWithID:1 andAge:2];
    24. }
    25. - (id) initWithID:(int)newID andAge:(int)newAge
    26. {
    27.      self = [super init];
    28.       if(self){
    29.       ID = newID;
    30.       age = newAge;
    31.       price = 60.0f;
    32.       }
    33.      return self;
    34. //return [self initWithID:1 andAge:2 andPrice:60.0f];
    35. }
    36. - (id) initWithID:(int)newID andAge:(int)newAge andPrice(float)newPrice
    37. {
    38.      self = [super init];
    39.       if(self){
    40.       ID = newID;
    41.       age = newAge;
    42.       price = newPrice;
    43.       }
    44.      return self;
    45. }//最终的构造函数
    46. - (void)setID:(int)newID
    47. {
    48.      ID = newID;
    49. }
    50. - (int)getID
    51. {
    52.      return ID;
    53. }
    54. - (void)setAge:(int)newAge
    55. {
    56.      age = newAge;
    57. }
    58. - (int)getAge
    59. {
    60.      return age;
    61. }
    62. - (void)setPrice:(float)newPrice
    63. {
    64.      price = newPrice;
    65. }
    66. - (float)getPrice
    67. {
    68.      return price;
    69. }
    70. - (void)setID:(int)newID andAge:(int)newAge
    71. {
    72.      ID = newID;
    73.      age = newAge;
    74. }
    75. - (void)setID:(int)newID andAge:(int)newAge andPrice(float)newPrice
    76. {
    77.      ID = newID;
    78.      age = newAge;
    79.      price = newPrice;
    80. }
    81. @end
    复制代码

    main.m
    1. #import <Foundation/Foundation.h>
    2. #import "Dog.h"
    3. int main(int argc, const char * argv[])
    4. {
    5. @autoreleasepool{
    6.       Dog *dog1 = [Dog alloc];
    7.       [dog1 init];
    8.       int ID = [dog1 getID];
    9.       int age = [dog1 getAge];
    10.       float price = [dog1 getPrice];
    11.       printf("dog1 id is %d age is %d price is %f \n",ID,age,price);
    12.       // dog1 id is 1 age is 2 price is 60.0000
    13.   
    14.       Dog *dog2 = [[Dog alloc] initWithID:100 andAge:36 andPrice:68.88];
    15.       int ID = [dog2 getID];
    16.       int age = [dog2 getAge];
    17.       float price = [dog2 getPrice];
    18.       printf("dog2 id is %d age is %d price is %f \n",ID,age,price);
    19.       //dog2 id is 100 age is 36 price is 68.879997
    20.   
    21.       Dog *dog2 = [[Dog alloc] setID:2012 andAge:38 andPrice:87.2];
    22.       int ID = [dog2 getID];
    23.       int age = [dog2 getAge];
    24.       float price = [dog2 getPrice];
    25.       printf("dog2 new id is %d age is %d price is %f \n",ID,age,price);
    26.       //dog2 net id is 2012 age is 38 price is 87.19998  
    27.      }
    28.      return 0
    29. }
    复制代码
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-11-23 20:11 , Processed in 0.044867 second(s), 19 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表