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

本人从今天开始学习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;
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值