oc面向對象編程——(類·方法·對象)

//--------------------------------------


#import <Foundation/Foundation.h>


@interface Rectangle : NSObject

{

    int length;

    int height;

}

@property(nonatomic) int length;

@property(nonatomic) int height;

-(int)setLength;

-(int)setHeight;

-(int) perimeter;

-(int) area;

@end

//

//  Rectangle.m

//  Rectangle

//

//  Created by 3-7 on 13-4-17.

//  Copyright (c) 2013 **. All rights reserved.

//-----------方法------------


#import "Rectangle.h"


@implementation Rectangle

@synthesize length;

@synthesize height;

-(void) setLength:(int)l

{

    length=l;

}

-(void) setHeight:(int)h

{

    height=h;

}

-(int) perimeter

{

    return (length+height)*2;

}

-(int) area

{

    return length*height;

    

}



@end

//

//  main.m

//  Rectangle

//

//  Created by 3-7 on 13-4-17.

//  Copyright (c) 2013 **. All rights reserved.

//***********對象***********


#import <Foundation/Foundation.h>

#import "Rectangle.h"


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

{


    @autoreleasepool {

        

        // insert code here...

        NSLog(@"Hello, World!");

        Rectangle *rectangle=[[Rectangle alloc]init];

        

        

        [rectangle setLength:20];

        [rectangle setHeight:20];

        NSLog(@"perimeter:%d",rectangle.perimeter);

        NSLog(@"area:%d",rectangle.area);

                

        

    }

    return 0;

}




發佈了23 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章