Object-c 學習之路一(Hello world)

來源:互聯網
上載者:User

本人從今天開始學習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;}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.