zoukankan      html  css  js  c++  java
  • ObjectiveC实例方法之多个参数声明与调用

    类接口文件(MathDiv.h)

    #import <Foundation/Foundation.h>
     
    //Define the Fraction class
     
    @interface Fraction: NSObject
    {
        int dividend;
        int divider;
    }
     
    @property int dividend, divider;
     
    -(void) print;
    -(void) setTo:(int)n over:(int)d;
    -(double) convertToNum;

    @end

    类实现文件(MathDiv.m)

    #import "Fraction.h"

    @implementation Fraction
     
    @synthesize dividend, divider;
     
    -(void) print
    {
        NSLog (@"%i/%i", dividend, divider);
    }
     
    -(double) convertToNum
    {
        if (divider != 0)
            return (double)dividend/divider;
        else
            return 0.0;
    }
     
    -(void) setTo:(int)n over: (int)d
    {
        dividend = n;
        divider = d;
    }
     
    @end

    主程序调用

    #import "Fraction.h"
     
    int main(int argc, const char * argv[])
    {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        Fraction *aFraction = [[Fraction alloc] init];
         
        [aFraction setTo: 100 over: 200];
        [aFraction print];
         
        [aFraction setTo: 1 over: 3];
        [aFraction print];
        
        [aFraction release];
      [pool drain];
        
        return 0;
    }

    运行结果:

    100/200

    1/3

    技术改变世界
  • 相关阅读:
    分享15个专业且免费的HTML5模板
    项目环境的搭建
    DNS预解析 dns-prefetch
    页面布局
    计划与准备
    Hogan的安装和使用
    代理工具--fiddle
    vue.js加入购物车小球动画
    vue.js笔记1.0
    url,href,src区别
  • 原文地址:https://www.cnblogs.com/davidgu/p/2523739.html
Copyright © 2011-2022 走看看