zoukankan      html  css  js  c++  java
  • Objectc 类的定义

    类定义文件:
     
    #import <Foundation/Foundation.h>
    
    //fraction :分数
    @interface Fraction : NSObject
    {//定义类得参数
        int numerator;    //分子
        int denominator;  //分母
    }
    
    
    -(void) print;
    -(void) setNumerator:   (int)n;
    -(void) setDenominator: (int)d;
    
    -(int) getNumerator;
    -(int) getDenominator;
    
    @end
     
     
    类实现文件:
    #import "Fraction.h"
    
    @implementation Fraction
    
    - (id)init
    {
        self = [super init];
        if (self) {
            // Initialization code here.
        }
        
        return self;
    }
    
    -(void) print{
        NSLog(@"%i/%i",numerator,denominator);
    }
    
    
    -(void) setNumerator:(int)n
    {
        numerator = n;
    }
    
    -(void) setDenominator:(int)d
    {
        denominator = d;
    }
    
    -(int)getNumerator
    {
        return numerator;
    }
    
    -(int)getDenominator
    {
        return denominator;
    }
    
     
    主实现文件:
     
    int main (int argc, const char * argv[])
    {
    
        // insert code here...
        //初始化类  
        Fraction *fraction = [[Fraction alloc]init];
        
        //调用类的方法
        [fraction setNumerator:1];
        [fraction setDenominator:3];
        [fraction print];
        
        int d = fraction.getDenominator;            
        return 0;
    }
  • 相关阅读:
    CF819B Mister B and PR Shifts
    HDU5969 最大的位或
    UVA1464 Traffic Real Time Query System
    [SCOI2010]连续攻击游戏
    [USACO11JAN] Roads and Planes
    [POJ3613] Cow Relays(Floyd+矩阵快速幂)
    洛谷P3237 [HNOI2014]米特运输(树形dp)
    awk 正则表达式、正则运算符详细介绍
    awk单行脚本快速参考
    Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
  • 原文地址:https://www.cnblogs.com/zhangweia/p/2231549.html
Copyright © 2011-2022 走看看