fork download
  1. #import <Foundation/NSObject.h>
  2. #import <stdio.h>
  3.  
  4. @interface Animal : NSObject
  5. {
  6. const char * name;
  7. }
  8. - (void) play;
  9. @end
  10.  
  11. @implementation Animal
  12. - (void) play {}
  13. @end
  14.  
  15. @interface Dog : Animal
  16. - (id) init: (const char *) name;
  17. - (void) play;
  18. @end
  19.  
  20. @implementation Dog
  21. - (id) init: (const char *) value {
  22. self = [super init];
  23. if (self != nil) {
  24. self->name = value;
  25. }
  26. return self;
  27. }
  28. - (void) play {
  29. printf("わんわん!%sだよ\n", name);
  30. }
  31. @end
  32.  
  33. @interface Cat : Animal
  34. {
  35. int sleep;
  36. }
  37. - (id) init: (const char *) name flag: (int) sleep;
  38. - (void) play;
  39. @end
  40.  
  41. @implementation Cat
  42. - (id) init: (const char *) value flag: (int) flag {
  43. self = [super init];
  44. if (self != nil) {
  45. self->name = value;
  46. self->sleep = flag;
  47. }
  48. return self;
  49. }
  50. - (void) play {
  51. printf("にゃあ!%sだよ\n", name);
  52. if(sleep == 1) {
  53. printf("...でも寝る\n");
  54. }
  55. }
  56. @end
  57.  
  58.  
  59. int main(int argc, char *argv[]) {
  60. int i;
  61. id pets[] = {
  62. [[Dog alloc] init:"ぽち"],
  63. [[Cat alloc] init:"たま" flag:1]
  64. };
  65.  
  66. for(i = 0; i < (sizeof(pets)/sizeof(pets[0])); i++) {
  67. [pets[i] play];
  68. }
  69.  
  70. for(i = 0; i < (sizeof(pets)/sizeof(pets[0])); i++) {
  71. [pets[i] release];
  72. }
  73. return 0;
  74. }
  75.  
Success #stdin #stdout 0.02s 14304KB
stdin
Standard input is empty
stdout
わんわん!ぽちだよ
にゃあ!たまだよ
...でも寝る