OC 示例2

建立一个Book类
Book.h

#import 
{
  NSString *_title;
  NSSInteger _pageNum;
}

- (void)showDetail;
- (instancetype)initWithTitle:(NSString *)title withPageNum:(NSInteger)pageNum;

@end

Book.m

#import "Book.h"
@implementation Book

- (instancetype)initWithTitle:(NSString *)title withPageNum:(NSInteger)pageNum
{
  if(self = [super init])
  {
    _title = title;
    _pageNum = pageNum;
  }
  return self;
}

- (void)showDetail
{
  NSLog(@"书名:%@ 页数:%ld",_title,_pageNum);
}

@end

main.m

#import 
#import "Book.h"

int main(int argc, const char *argv[])
{
  @autoreleasepool
  {
    Book *book = [[Book alloc]initWithTitle:@"三体" withPageNum:100];
    [book showDetail];
  }
  return 0;
}

你可能感兴趣的:(OC 示例2)