Home > Objective C > Objective Cはおもしろい!

Objective Cはおもしろい!

Objective Cのプログラム書いてるけど、オブジェクト同士がまさに対話しているかのようだ。
【ダウンロード】 詳解 Objective-C 2.0
上記のURLでソースを落とせるみたい。

#import <stdio.h>
#import <Foundation/NSObject.h>

@interface Volume : NSObject
{
  int val;
  int min, max, step;
}

- (id)initWithMin:(int) a max:(int)b step:(int)s;
- (int)value;
- (id)up;
- (id)down;
@end

@implementation Volume
- (id)initWithMin:(int)a max:(int)b step:(int)s
{
  self = [super init];
  if (self != nil) {
    val = min = a;
    max = b;
    step = s;
  }
  return self;
}

- (int)value
{
  return val;
}

- (id)up
{
  if ((val += step) > max)
    val = max;
  return self;
}

- (id)down
{
  if ((val -= step) < min)
    val = min;
  return self;
}
@end

int main() {
  id v, w;

  v = [[Volume alloc] initWithMin:0 max:10 step:2];
  w = [[Volume alloc] initWithMin:0 max:9 step:3];
  [v up];
  printf("%d %d\n", [v value], [w value]);
  [v up];
  [w up];
  printf("%d %d\n", [v value], [w value]);
  [v down];
  [w down];
  printf("%d %d\n", [v value], [w value]);
  return 0;
}

windows:objc yup$ gcc -framework Foundation volume.m
windows:objc yup$ ./a.out
2 0
4 3
2 0


Similar Posts:

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://plavs.com/2009/10/26/objective-c%e3%81%af%e3%81%8a%e3%82%82%e3%81%97%e3%82%8d%e3%81%84/trackback/
Listed below are links to weblogs that reference
Objective Cはおもしろい! from Yupの開発日記

Home > Objective C > Objective Cはおもしろい!

Return to page top