IPhone开发(2) 一个比较完整的类

在Java里面,类和接口没有直接的依赖关系,我的意思是,写一个类,我们不必声明它的接口,但是obC里面是不行的。每一个类,必须有显式的接口说明。下面直接上代码:


接口文件:Fraction.h:

//
//  Fraction.h
//  first
//
//  Created by coding on 12-2-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Fraction : NSObject
{
    int numerator;
    int denominator;
}
-(void) print;
-(void) setNumerator:(int)n;
-(void) setDenominator:(int)d;

@end

类文件:Fraction.m

//
//  Fraction.m
//  first
//
//  Created by coding on 12-2-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
-(void) print
{
    NSLog(@"%i/%i",numerator,denominator);
}

-(void) setNumerator:(int)n
{
    numerator = n;
}

-(void) setDenominator:(int)d
{
    denominator = d;
}
@end

主运行文件:main.m

//
//  main.m
//  first
//
//  Created by coding on 12-2-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#include "Fraction.h"
int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        Fraction *myFraction;   
        myFraction = [Fraction new];
        [myFraction setNumerator:1];
        [myFraction setDenominator:3];
        [myFraction print];
    }
    return 0;
}

//方法的调用语法:[ClassOrInstance method];


你可能感兴趣的:(java,c,iPhone,interface)