现在的位置: 首页 > 综合 > 正文

Object-c 学习之路一(Hello world)

2014年01月08日 ⁄ 综合 ⁄ 共 1616字 ⁄ 字号 评论关闭

本人从今天开始学习oc 了心里灰常高兴,写了个hello world 留作纪念。

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject
{

    int width;
    int height;


}
-(void) setWidth:(int) w setGeight:(int) h;
-(int) gePerimeter;
-(int) getArea;

@end

#import "Rectangle.h"

@implementation Rectangle

-(void) setWidth:(int) w setGeight:(int) h{

    width=w;
    height=h;
}
-(int) gePerimeter{
    return (width+height)*2;
}
-(int) getArea{
    return width*height;
}

@end

Square 类继承Rectangle类

#import "Rectangle.h"

@interface Square : Rectangle

-(void) setSide:(int) w;
-(int) side;

@end

#import "Square.h"

@implementation Square:Rectangle
-(void) setSide:(int)w
{
    [self setWidth:w setGeight:w];

}
-(int) side{
    return width;
}
-(int) getArea{
    return [self side]*[self side];
}



@end

主文件:

//
//  main.m
//  OcStart
//
//  Created by WildCat on 13-3-25.
//  Copyright (c) 2013年 wildcat. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Rectangle.h"
#import "Square.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Hello, World!");
        NSLog(@"李兴乐你好!!");
    
        
        //定义一个Person类
        Person * p=[[Person alloc] init];
        [p setStr:@"李兴乐"];
        [p myPrint];
        //定义一个Rectangle类对象
        Rectangle *r=[[Rectangle alloc] init];
        
        [r setWidth:3 setGeight:4];
        int area=[r getArea];
        int perimeter=[r gePerimeter];
        NSLog(@"The Area is:%d ,the parimeter is %d",area,perimeter);
        //定义一个Square实例对象
        Square *s=[[Square alloc] init];
        //键盘输入正方型的边长
        NSLog(@"请输入正方形的边长");
        int number;
        scanf("%i",&number);
        //设置边长
        [s setSide:number];
        int side=[s side];
        int areas=[s getArea];
        int perimeters=[s gePerimeter];
        NSLog(@"The square's side is :%d ,the area is : %d ,the primeter is : %d",side,areas,perimeters);
        
        NSString *hello=@"sdfgdsf";
        //转换为大写
        hello=[hello uppercaseString];
        NSLog(hello);
        //调用字符串的求长度方法
        NSLog(@"The String length is : %ld",[hello length]);
      
        
        
    }
    return 0;
}

抱歉!评论已关闭.