OC簡單知識的練習

習題:

利用Objective-C語言編寫一個Screen類,它包括一個int類型的屬性color,和兩個對color進行操作的方法。其中一個是用於設置color值的方法setColor,方法沒有返回值,形參是一個int類型的變量_color,方法的實現是將_color的值賦值給color。還有一個是用於顯示color值的方法displayColor,它是一個沒有參數和返回值的方法,方法的實現是顯示color的值。

(1)     在主函數中創建Screen類的對象myScreen。。

(2)     然後從鍵盤上接收一個整數,利用發送消息的方式調用myScreen的setColor方法,將這個整數發送給myScreen,完成對屬性color的賦值。

(3)     調用對象myScreen的displayColor方法輸出color的值。


//
//  Screen.h
//  OC_03面向對象編程基礎4.1.1任務描述
//
//  Created by 谷建鵬 on 14-3-19.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//


#import <Foundation/Foundation.h>
@interface Screen : NSObject
{
    int  color;
}

//用於設置color的變量方法;
//無返回值;
-(void)setColor:(int) _color;

//用於設置color變量的調用方法用displayColor;
-(void)displayColor;

@end


//
//  Screen.m
//  OC_03面向對象編程基礎4.1.1任務描述
//
//  Created by 谷建鵬 on 14-3-19.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//


#import "Screen.h"
@implementation Screen
//設置形參數,_color;
-(void)setColor:(int)_color{
   //將_color的值賦值給color
    color = _color; 
}

//用於顯示color的調用方法;沒有參數和返回值的參數。。
-(void)displayColor{
    NSLog(@"您輸入一個數是:\ncolor = %d\n",color);
}

@end


//
//  main.m
//  OC_03面向對象編程基礎4.1.1任務描述
//
//  Created by 谷建鵬 on 14-3-19.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//


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


    @autoreleasepool {
       
        //創建一個Scree類的對象screen;
        Screen *screen = [[Screen alloc] init];
       
        //定義color;
        int color;
        //從鍵盤上插入一個color;
        scanf("請您輸入一個數:%d",&color);
        [screen setColor:color];
        [screen displayColor];
        
        
        
    }
    return 0;
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

簡單的四則運算問題

//
//  Calculator.h
//  簡單運算
//
//  Created by 谷建鵬 on 14-3-20.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//


#import <Foundation/Foundation.h>


@interface Calculator : NSObject


//定義設置初始累加數的方法setAccmulator
-(void) setAccmulator:(double) val;
-(void) clear;  //清零累加數的方法
-(double) accmulator;


-(void) add:(double) val;
-(void) subtract:(double) val;
-(void) multiply:(double) val;
-(void) divide:(double)val;
@end


//
//  Calculator.m
//  簡單運算
//
//  Created by 谷建鵬 on 14-3-20.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//






#import "Calculator.h"


@implementation Calculator
{
    double accmulator;
}


-(void) setAccmulator:(double) val
{
    accmulator=val;
}


-(void) clear
{
    accmulator=0;
}


-(double) accmulator
{
    return accmulator;
}


-(void) add:(double) val
{
    accmulator+=val;
}


-(void) subtract:(double) val
{
    accmulator-=val;
}


-(void) multiply:(double) val
{
    accmulator*=val;
}


-(void) divide:(double)val
{
    accmulator/=val;
}


@end




//
//  main.m
//  簡單運算
//
//  Created by 谷建鵬 on 14-3-20.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//


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


    @autoreleasepool {
        
        Calculator *calc = [Calculator new];
        
        [calc setAccmulator:10.0];
        [calc add:50.];
        [calc divide: 20.0];
        [calc multiply:40];
        [calc subtract:20];
        NSLog(@"The result is %g",[calc accmulator]);
        [calc clear];
        NSLog(@"The accmulator is %g",calc.accmulator);
        
    }
    return 0;
}


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


定義一個名爲Car的類,類中的屬性可以包含輪子轉速speed、輪子的半徑radius、汽車重量weight。爲Car類定義一些方法,要求能夠通過這些方法來設置類中的屬性值。創建該類的兩個實例化對象。定義一個函數,要求能夠計算出汽車的行駛速度並且顯示出來。

//
//  main.m
//  輪子轉速
//
//  Created by 谷建鵬 on 14-3-20.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//




#import <Foundation/Foundation.h>


@interface Car : NSObject


@property float speed,weight;
@property int radius;
-(void)display;


@end


@implementation Car
@synthesize speed,weight,radius;


-(void)display{
    NSLog(@"汽車的重量:%.2f,汽車的行駛速度:%.3f",weight,speed*2*3.14*radius);
}


@end


int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        Car *car = [[Car alloc] init];
        [car setSpeed:3.5];
        [car setRadius:3];
        [car setWeight:1000.55];
        [car speed];
        [car radius];
        [car weight];
        
        [car display];
    }
    return 0;
}


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


dealloc 方法的使用


//
//  Student.h
//  5_dealloc
//
//  Created by 谷建鵬 on 14-3-18.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//


#import <Foundation/Foundation.h>
@interface Student : NSObject
{
    int  age;
    NSString *s; //存字符串
}
-(void)setAge:(int) _age;
//定義一個方法
-(void)test;
@end


//
//  Student.m
//  5_dealloc
//
//  Created by 谷建鵬 on 14-3-18.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//


#import "Student.h"
@implementation Student
-(void)setAge:(int) _age{ 
    //申請一個內存空間,長度爲4個字節
    //s = (char *)malloc(5);
    //給s賦值爲csdn  
    s  = [[NSString alloc] initWithString:@"csdn"];
    age = _age;
}


-(void)test{
    //NSLog(@"age = %d,s = %@",age,s);
}

調用dealloc方法

-(void)dealloc{ 
    //先釋放父類的資源
    [super dealloc];
    NSLog(@"釋放資源!!!");
}

@end


//
//  main.m
//  5_dealloc
//
//  Created by 谷建鵬 on 14-3-18.
//  Copyright (c) 2014年 com.csdn. All rights reserved.
//


#import <Foundation/Foundation.h>
#import "Student.h"//引入頭文件
int main(int argc, const char * argv[])
{


    @autoreleasepool {
    
        //將Student進行實例化
        Student  *stu =[[Student alloc] init];
        //進行調用set方法
        [stu setAge:34];
        //打印輸出
        [stu test];
        
        [stu release];
        
    }
    return 0;
}



























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章