Третя заготовка на Objective-C.
У тексті ви бачите початковий код програми, а для завантаження доступний відкомпільований файл під Linux у архіві. Запускати із консолі(якщо
назва файлу "1" , то команда для запуску із папки із цим файлом буде
"./1").
#import <Foundation/Foundation.h>
@interface my_ant : NSObject { int live; int armor; int fire; int number; int mapx; }
-(void) set_live: (int) live; -(void) set_armor: (int) armor; -(void) set_fire: (int) fire; -(void) set_number: (int) number; -(int) get_number; -(void) set_mapx: (int) mapx; -(int) get_mapx; -(void) ant_print;
@end //@interface my_ant
@implementation my_ant
-(void) set_live: (int) l { live = l; }
-(void) set_armor: (int) a { armor = a; }
-(void) set_fire: (int) f { fire = f; }
-(void) set_number: (int) n { number = n; }
-(int) get_number { return number; }
-(void) set_mapx: (int) mx { mapx = mx; }
-(int) get_mapx { return mapx; }
-(NSString *) description { return (@"Live Armor Fire Number Mapx"); }
-(void) ant_print { NSLog (@"Live = %d; Armor = %d; Fire = %d; Number = %d; MapX = %d", live, armor, fire, number, mapx); }
@end //implementation my_ant
@interface ant_work : my_ant { } -(void) ant_do: (int) mapx;
@end //@interface ant_work
@implementation ant_work -(void) ant_do: (int) mx { NSLog(@"I go work at %d!", mx); } @end //@implementation ant_work
@interface ant_soldier : my_ant { } -(void) ant_do: (int) mapx; @end //@interface ant_soldier
@implementation ant_soldier -(void) ant_do: (int) mx { NSLog (@"I go fire at %d", mx); } @end //@implementation ant_soldier
@interface ant_spy: my_ant { } -(void) ant_do: (int) mapx; @end //@interface ant_spy
@implementation ant_spy -(void) ant_do: (int) mx { NSLog(@"I go spy at %d", mx); } @end //@implementation ant_spy
@interface ant_player: NSObject { ant_work *ant_works; ant_soldier *ant_soldiers; ant_spy *ant_spys;
NSString * player_name; }
-(ant_work *) ant_works; -(void) set_ant_work: (ant_work *) new_ant_works; -(ant_soldier *) ant_soldiers; -(void) set_ant_soldier: (ant_soldier *) new_ant_soldiers; -(ant_spy *) ant_spys; -(void) set_ant_spy: (ant_spy *) new_ant_spys;
-(void) set_name: (NSString *) player_name; -(void) player_print; @end //@interface ant_player
@implementation ant_player
-(ant_work *) ant_works { return ant_works; } -(void) set_ant_work: (ant_work *) new_ant_works { ant_works = new_ant_works; } -(ant_soldier *) ant_soldiers { return ant_soldiers; } -(void) set_ant_soldier: (ant_soldier *) new_ant_soldiers { ant_soldiers = new_ant_soldiers; } -(ant_spy *) ant_spys { return ant_spys; } -(void) set_ant_spy: (ant_spy *) new_ant_spys { ant_spys = new_ant_spys; }
-(void) set_name: (NSString *) pn { player_name = pn; }
-(void) player_print { NSLog (@"Player name: -=%@=- have - %@ %@ %@",player_name , ant_works, ant_soldiers, ant_spys); } @end //@implementation ant_player
int main (int argc, const char * argv[]) { ant_work * ant_works = [ant_work new]; [ant_works set_live: 5]; [ant_works set_armor: 5]; [ant_works set_fire: 3]; [ant_works set_number: 1]; [ant_works ant_print]; [ant_works ant_do: 1];
ant_soldier * ant_soldiers = [ant_soldier new]; [ant_soldiers set_live: 15]; [ant_soldiers set_armor: 15]; [ant_soldiers set_fire: 5]; [ant_soldiers set_number: 0]; [ant_soldiers ant_print]; [ant_soldiers ant_do: 1];
ant_spy * ant_spys = [ant_spy new]; [ant_spys set_live: 5]; [ant_spys set_armor: 20]; [ant_spys set_fire: 3]; [ant_spys set_number: 0]; [ant_spys ant_print]; [ant_spys ant_do: 1];
ant_player * ant_player1 = [ant_player new]; [ant_player1 set_ant_work: ant_works]; [ant_player1 set_ant_soldier: ant_soldiers]; [ant_player1 set_ant_spy: ant_spys]; [ant_player1 set_name: @"Hello"]; [ant_player1 player_print];
return 0; }
|