Objective-Cでダックタイピング
こんだけ読んだ
Duck Typingといえば Objective-C だよねー
ということで Objective-C で書いてみた
#import <stdio.h> #import <objc/Object.h> @interface Cat : Object - (void) speak; @end @interface Dog : Object - (void) speak; @end @implementation Cat - (void) speak { printf("meow!\n"); } @end @implementation Dog - (void) speak { printf("woof!\n"); } @end int main() { int i, n_pets; id pets[] = {[Cat alloc], [Dog alloc]}; n_pets = sizeof(pets) / sizeof(pets[0]); for (i = 0; i < n_pets; i++) { [pets[i] speak]; } return 0; }
Objective-C だと getClass().getMethod(〜) とかいらない