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;
    }
  • 相关阅读:
    bt5设置IP
    flyCoding
    [Cocoa][译]苹果 Cocoa 编码规范中文版
    [BZOJ4569] [Scoi2016]萌萌哒
    BZOJ4899]记忆的轮廓
    [BZOJ1701] [Usaco2007 Jan]Cow School牛学校
    [Poi2011]Lightning Conductor
    [BZOJ4709] [Jsoi2011] 柠檬
    决策单调性优化dp 专题练习
    2369. 区间
  • 原文地址:https://www.cnblogs.com/zhangweia/p/2231549.html
Copyright © 2011-2022 走看看