• 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

    技术改变世界
  • 相关阅读:
    2月16号
    2月15号
    dubbo与springmvc的简单使用
    dubbo与zookeeper学习中的问题
    linux下jdk与tomcat的安装与配置
    mysql存储引擎
    mysql存储过程
    mysql子查询与连接查询
    mysql简单增删改查(CRUD)
    SpringMvc执行流程
  • 原文地址:https://www.cnblogs.com/davidgu/p/2523739.html
Copyright © 2020-2023  润新知