年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2104|回复: 0

第四讲:什么叫做“类”之构造函数

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2013-12-12 22:33:27 | 显示全部楼层 |阅读模式
    Fraction.h
    1. #import <Foundation/NSObject.h>
    2.   
    3. @interface Fraction: NSObject  
    4. {
    5.     int numerator;  
    6.     int denominator;  
    7. }  
    8.   
    9. -(void) print;
    10. -(void) setNumerator: (int) n;
    11. -(void) setDenominator: (int) d;
    12. -(int) numerator;
    13. -(int) denominator;
    14. -(void) setNumerator: (int) n andDenominator: (int) d;
    15. //****************************************************
    16. -(Fraction*) initWithNumerator:(int)n denominator: (int) d;
    17. //****************************************************
    18. @end
    复制代码

    Fraction.m
    1. #import "Fraction.h"
    2. #import <stdio.h>  
    3. @implementation Fraction
    4. -(void) print
    5. {
    6.     printf("%i/%i",numerator,denominator);
    7. }
    8.   
    9. -(void) setNumerator: (int) n
    10. {   
    11.     numerator = n;
    12. }
    13. -(void) setDenominator: (int) d
    14. {   
    15.     denominator = d;
    16. }
    17.   
    18. -(int) denominator
    19. {   
    20.     return denominator;
    21. }
    22. -(int) numerator
    23. {   
    24.     return numerator;
    25. }
    26. -(void) setNumerator: (int) n andDenominator: (int) d
    27. {  
    28.     numerator = n;  
    29.     denominator = d;
    30. }
    31. //**********************************************************
    32. -(Fraction*) initWithNumerator: (int) n denominator: (int) d
    33. {  
    34.     self = [super init];
    35.     if ( self ) {
    36.     [self setNumerator: n andDenominator: d];  
    37.     }
    38.     return self;
    39. }
    40. //**********************************************************
    41. @end
    复制代码

    main.m
    1. #import <stdio.h>
    2. #import "Fraction.h"  
    3. int main( int argc, const char *argv[] )
    4. {
    5.     Fraction *frac = [[Fraction alloc] init];  
    6.     [frac setNumerator: 1];  
    7.     [frac setDenominator: 3];
    8.     printf( "The fraction is: " );  
    9.     [frac print];
    10.     [frac release];
    11.     Fraction *frac2 = [[Fraction alloc] init];
    12.     [frac2 setNumerator: 1 andDenominator: 5];
    13.     printf( "Fraction 2 is: " );  
    14.     [frac2 print];
    15.     [frac2 release];
    16. //***************************************************
    17.     Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
    18.     printf( "Fraction 3 is: " );
    19.     [frac3 print];
    20.     [frac3 release];
    21. //***************************************************  
    22.     return 0;
    23. }
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-11-23 19:38 , Processed in 0.046136 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表