저는 사각형을 정의하고 면적과 둘레를 계산하면서 너비와 높이를 조작 할 수있는 기본 지오메트리 클래스를 설정하고 있습니다. 경계선과 영역 변수가 0으로 돌아가는 것을 제외하면 모든 것이 올바르게 작동하고 출력됩니다. 변수 자체를 올바르게 설정하는 법이나 @implementation
중에 변수를 설정하는 방법을 모르므로 변수가 처음 초기화 될 때 (너비와 높이가 설정되기 전에) 0을 표시하고 있다고 확신합니다.계산/변수가 0으로 돌아 오는 변수
저는 OOP와 ObjC에 익숙하지 않아서 간단한 것을 놓칠 수 있습니다.
#import <Foundation/Foundation.h>
// @interface setup as required.
@interface Rectangle: NSObject
-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(int) width;
-(int) height;
-(int) area;
-(int) perimeter;
-(void) print;
@end
// @implementation setup for the exercise.
@implementation Rectangle {
int width;
int height;
int perimeter;
int area;
}
// Set the width.
-(void) setWidth: (int) w {
width = w;
}
// Set the height.
-(void) setHeight: (int) h {
height = h;
}
// Calculate the perimeter.
-(int) perimeter {
return (width + height) * 2;
}
// Calculate the area.
-(int) area {
return (width * height);
}
-(void) print {
NSLog(@"The width is now: %i.", width);
NSLog(@"The height is now: %i.", height);
NSLog(@"The perimeter is now: %i.", perimeter);
NSLog(@"The area is now: %i.", area);
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Create an instance of Rectangle.
Rectangle *theRectangle;
theRectangle = [Rectangle alloc];
theRectangle = [theRectangle init];
// Use the designed methods.
[theRectangle setWidth: 100];
[theRectangle setHeight: 50];
[theRectangle print];
}
return 0;
}
재산 –
당신이 오래된 자습서를 따르고 있습니다이를 확인 바르를 사용하지 않는? 이 클래스 선언은 현대 기능을 사용하지 않습니다. –
예. 책에서 나는 그것을 들고 일반 연습을 사용했다. 머리를 가져 주셔서 감사합니다. 나는 책에서 낡은 것보다 당신이 말하는 것처럼 제대로 배우고 싶습니다. 체크 아웃 할만한 좋은 자료를 알고 있습니까? – user3223880