SlideShare a Scribd company logo
1 of 28
Objective-C




                Objective-C에서의 OOP
                By Changhoon Park
                http://changhoonpark.wordpress.com
                                                     Last Update : 2011. 08. 28

11년 9월 7일 수요일
@interface 클래스명 : 슈퍼클래스명 {

          ! !       /* 인스턴스 변수 선언 */

          }

          ! !       /* 메소드 선언 */

          @end




                2

                     OOP   @interface 색션
                           @implement 색션
                           인스턴스 만들기
                           실습



11년 9월 7일 수요일
@interface 클래스명 : 슈퍼클래스명 {

          ! !       /* 인스턴스 변수 선언 */

          }

          ! !       /* 메소드 선언 */
                                           objC의 클래스는 @interface와
          @end                                 @end 사이에 선언됨




                3

                     OOP   @interface 색션
                           @implement 색션
                           인스턴스 만들기
                           실습



11년 9월 7일 수요일
@interface 클래스명 : 슈퍼클래스명 {

          ! !       /* 인스턴스 변수 선언 */

          }

          ! !       /* 메소드 선언 */
                                          objC의 클래스 이름은
          @end                         @interface 바로 다음에 나옴




                4




11년 9월 7일 수요일
@interface 클래스명 : 슈퍼클래스명 {

          ! !       /* 인스턴스 변수 선언 */

          }

          ! !       /* 메소드 선언 */
                                           상위클래스는 콜론 다음부모
          @end                              클래스의 이름으로 명시




                5

                     OOP   @interface 색션
                           @implement 색션
                           인스턴스 만들기
                           실습



11년 9월 7일 수요일
@interface 클래스명 : 슈퍼클래스명 {

          ! !       /* 인스턴스 변수 선언 */

          }

          ! !       /* 메소드 선언 */
                                       중괄호 사이에 객체 변수
          @end                         (Instance Variable) 선언




                6




11년 9월 7일 수요일
@interface 클래스명 : 슈퍼클래스명 {

          ! !       /* 인스턴스 변수 선언 */

          }

          ! !       /* 메소드 선언 */
                                           메소드는 @interface와 @end
          @end                              사이 & 중괄호 밖에 선언




                7

                     OOP   @interface 색션
                           @implement 색션
                           인스턴스 만들기
                           실습



11년 9월 7일 수요일
- (int) age;

          - (void) setAge: (int) age;

          + (Person) personWithName: (NSString*) name age: (int) age:




                    - : 인스턴스 메소드 표시
                    + : 클래스 메소드 표시




                8

                      OOP   @interface 색션
                            @implement 색션
                            인스턴스 만들기
                            실습



11년 9월 7일 수요일
- (int) age;

          - (void) setAge: (int) age;

          + (Person) personWithName: (NSString*) name age: (int) age:




           반환 타입을 괄호 안에 표시




                9

                    OOP   @interface 색션
                          @implement 색션
                          인스턴스 만들기
                          실습



11년 9월 7일 수요일
- (int) age;

          - (void) setAge: (int) age;

          + (Person) personWithName: (NSString*) name age: (int) age:




                     메소드 이름




           10

                    OOP   @interface 색션
                          @implement 색션
                          인스턴스 만들기
                          실습



11년 9월 7일 수요일
- (int) age;

          - (void) setAge: (int) age;

          + (Person) personWithName: (NSString*) name age: (int) age:




           콜론(:)은 다음에 인수가 있음
                   을 표시




            11




11년 9월 7일 수요일
- (int) age;

          - (void) setAge: (int) age;

          + (Person) personWithName: (NSString*) name age: (int) age:




          괄호 안에 인수의 유형을 표시




           12

                    OOP   @interface 색션
                          @implement 색션
                          인스턴스 만들기
                          실습



11년 9월 7일 수요일
- (int) age;

          - (void) setAge: (int) age;

          + (Person) personWithName: (NSString*) name age: (int) age:




                     인수의 이름




           13

                    OOP   @interface 색션
                          @implement 색션
                          인스턴스 만들기
                          실습



11년 9월 7일 수요일
@interface Circle : NSObject
          {
            ShapeColor fillColor;
            ShapeRect bounds;
          }

          - (void) setFillColor: (ShapeColor) fillColor;

          - (void) setBounds: (ShapeRect) bounds;

          - (void) draw;

          @end // Circle



           14




11년 9월 7일 수요일
@implementation 클래스_이름


          ! !    /* 메소드 정의 */


          @end                             objC의 클래스 이름은
                                        @implementation 바로 다음에




           15

                  OOP   @interface 색션
                        @implement 색션
                        인스턴스 만들기
                        실습



11년 9월 7일 수요일
@implementation 클래스_이름


          ! !    /* 메소드 정의 */


          @end                          메소드는 @implementation와
                                          @end 사이에 정의 됨




           16

                  OOP   @interface 색션
                        @implement 색션
                        인스턴스 만들기
                        실습



11년 9월 7일 수요일
@implementation Circle
          - (void) setFillColor: (ShapeColor) c {
              fillColor = c;
          } // setFillColor

          - (void) setBounds: (ShapeRect) b {
              bounds = b;
          } // setBounds

          - (void) draw {
              NSLog (@"drawing a circle at (%d %d %d %d) in %@",
                    bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor));
          } // draw
          @end // Circle




           17

                    OOP     @interface 색션
                            @implement 색션
                            인스턴스 만들기
                            실습



11년 9월 7일 수요일
int main (int argc, const char * argv[])
                                      {
 ! [객체 메시지_이름: 인수];                   ! id shapes[3];
                                      !
                                      ! ShapeRect rect0 = { 0, 0, 10, 30 };
                                      ! shapes[0] = [Circle new];
                                      ! [shapes[0] setBounds: rect0];
                                      ! [shapes[0] setFillColor: kRedColor];
                                      !
                                      ! ShapeRect rect1 = { 30, 40, 50, 60 };
                                      ! shapes[1] = [Rectangle new];
                                      ! [shapes[1] setBounds: rect1];
                                      ! [shapes[1] setFillColor: kGreenColor];
                                      !
                                      ! ShapeRect rect2 = { 15, 19, 37, 29 };
                                      ! shapes[2] = [OblateSphereoid new];
                                      ! [shapes[2] setBounds: rect2];
                                      ! [shapes[2] setFillColor: kBlueColor];
                                      !
                                      ! drawShapes (shapes, 3);
                                      !
                                      ! return (0);
                                      !
           18                         } // main


                OOP   @interface 색션
                      @implement 색션
                      인스턴스 만들기
                      실습



11년 9월 7일 수요일
!     [shapes[0] setBounds: rect0];
          !     [shapes[0] setFillColor: kRedColor];
          !
          !     [shapes[1] setBounds: rect1];
          !     [shapes[1] setFillColor: kGreenColor];
          !
          !     [shapes[2] setBounds: rect2];
          !     [shapes[2] setFillColor: kBlueColor];




           ! [객체 메시지_이름: 인수];




              19

                     OOP   @interface 색션
                           @implement 색션
                           인스턴스 만들기
                           실습



11년 9월 7일 수요일
shapes[0] = [Circle new];




            [클래스_이름 new];




           20

                OOP   @interface 색션
                      @implement 색션
                      인스턴스 만들기
                      실습



11년 9월 7일 수요일
! id shapes[3];


                shapes[0] = [Circle new];

          ! shapes[1] = [Rectangle new];
          !
          ! shapes[2] = [OblateSphereoid new];




           21

                    OOP   @interface 색션
                          @implement 색션
                          인스턴스 만들기
                          실습



11년 9월 7일 수요일
void drawShapes (id shapes[], int count)
          {
          ! int i;
          !
          ! for (i = 0; i < count; i++) {
          ! ! id shape = shapes[i];
          ! ! [shape draw];
          ! }
          !
          } // drawShapes




           22

                OOP   @interface 색션
                      @implement 색션
                      인스턴스 만들기
                      실습



11년 9월 7일 수요일
1. Triangle 클래스 추가하기 (p70)

          2. main 함수 수정 (p71)

          결과 화면




           23

                OOP   @interface 색션   Shape 확장하기
                      @implement 색션   MyClass 따라하기
                      인스턴스 만들기
                      실습



11년 9월 7일 수요일
1. Application을 생성합니다.
           - Command Line Tool (Type는 반드시 Foundation으로!)

        2. 실행하여 Hello world!가 호출되는지 확인합니다.

        3. MyClass라는 클래스를 만들어 봅니다.
        (Mac OS X - Cocoa - Objectice-C class - Subclass of : NSObject
        - MyClass를 선택하여 myclass.mm, myclass.h를 생성)




           24

                OOP   @interface 색션   Shape 확장하기
                      @implement 색션   MyClass 따라하기
                      인스턴스 만들기
                      실습



11년 9월 7일 수요일
MyClass의 인스턴스를 생성하기
            - new, alloc의 방법을 사용




           25

                OOP   @interface 색션   Shape 확장하기
                      @implement 색션   MyClass 따라하기
                      인스턴스 만들기
                      실습



11년 9월 7일 수요일
MyClass의 메소드 추가하기.
            - class method, instance method 모두 추가




           26

                 OOP   @interface 색션   Shape 확장하기
                       @implement 색션   MyClass 따라하기
                       인스턴스 만들기
                       실습



11년 9월 7일 수요일
MyClass의 메소드 호출하기
            - class method, instance method 모두 호출




           27

                 OOP   @interface 색션   Shape 확장하기
                       @implement 색션   MyClass 따라하기
                       인스턴스 만들기
                       실습



11년 9월 7일 수요일
1. 추가한 메소드에 매개변수를 넘겨봅시다.
            - class method, instance method에 매개변수 만들기
            - int, float, NSString 등의 여러가지 데이터 타입 이용




           28

                 OOP   @interface 색션   Shape 확장하기
                       @implement 색션   MyClass 따라하기
                       인스턴스 만들기
                       실습



11년 9월 7일 수요일

More Related Content

Similar to Objective-C에서의 OOP

Similar to Objective-C에서의 OOP (10)

Object C - RIP
Object C - RIPObject C - RIP
Object C - RIP
 
Hello Swift 4/5 : Closure and Enum
Hello Swift 4/5 : Closure and EnumHello Swift 4/5 : Closure and Enum
Hello Swift 4/5 : Closure and Enum
 
[자바학원/스프링교육학원/마이바티스학원추천/구로IT학원_탑크리에듀]#7.스프링프레임워크 & 마이바티스 (Spring Framework, M...
[자바학원/스프링교육학원/마이바티스학원추천/구로IT학원_탑크리에듀]#7.스프링프레임워크 & 마이바티스 (Spring Framework, M...[자바학원/스프링교육학원/마이바티스학원추천/구로IT학원_탑크리에듀]#7.스프링프레임워크 & 마이바티스 (Spring Framework, M...
[자바학원/스프링교육학원/마이바티스학원추천/구로IT학원_탑크리에듀]#7.스프링프레임워크 & 마이바티스 (Spring Framework, M...
 
Angular2 가기전 Type script소개
 Angular2 가기전 Type script소개 Angular2 가기전 Type script소개
Angular2 가기전 Type script소개
 
Why what how kotlin
Why what how kotlinWhy what how kotlin
Why what how kotlin
 
자바스크립트 클래스의 프로토타입(prototype of class)
자바스크립트 클래스의  프로토타입(prototype of class)자바스크립트 클래스의  프로토타입(prototype of class)
자바스크립트 클래스의 프로토타입(prototype of class)
 
8.Spring DI_3
8.Spring DI_38.Spring DI_3
8.Spring DI_3
 
Javascript 교육자료 pdf
Javascript 교육자료 pdfJavascript 교육자료 pdf
Javascript 교육자료 pdf
 
I phone 2 release
I phone 2 releaseI phone 2 release
I phone 2 release
 
01.실행환경 실습교재(공통기반)
01.실행환경 실습교재(공통기반)01.실행환경 실습교재(공통기반)
01.실행환경 실습교재(공통기반)
 

More from Hoseo University (13)

Game ai.fsm.02
Game ai.fsm.02Game ai.fsm.02
Game ai.fsm.02
 
Game ai.fsm.01
Game ai.fsm.01Game ai.fsm.01
Game ai.fsm.01
 
Game math.points and lines
Game math.points and linesGame math.points and lines
Game math.points and lines
 
Esl podcast 743 – writing a story
Esl podcast 743 – writing a storyEsl podcast 743 – writing a story
Esl podcast 743 – writing a story
 
Property
PropertyProperty
Property
 
목적이 부여된 에이전트 행동
목적이 부여된 에이전트 행동목적이 부여된 에이전트 행동
목적이 부여된 에이전트 행동
 
실질적인 길 계획하기
실질적인 길 계획하기실질적인 길 계획하기
실질적인 길 계획하기
 
FoundationKit
FoundationKitFoundationKit
FoundationKit
 
Raven
RavenRaven
Raven
 
프로젝트 구성
프로젝트 구성프로젝트 구성
프로젝트 구성
 
구성(Composition)
구성(Composition)구성(Composition)
구성(Composition)
 
Dt2210.01.syllabus
Dt2210.01.syllabusDt2210.01.syllabus
Dt2210.01.syllabus
 
Dt3160.01
Dt3160.01Dt3160.01
Dt3160.01
 

Objective-C에서의 OOP

  • 1. Objective-C Objective-C에서의 OOP By Changhoon Park http://changhoonpark.wordpress.com Last Update : 2011. 08. 28 11년 9월 7일 수요일
  • 2. @interface 클래스명 : 슈퍼클래스명 { ! ! /* 인스턴스 변수 선언 */ } ! ! /* 메소드 선언 */ @end 2 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 3. @interface 클래스명 : 슈퍼클래스명 { ! ! /* 인스턴스 변수 선언 */ } ! ! /* 메소드 선언 */ objC의 클래스는 @interface와 @end @end 사이에 선언됨 3 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 4. @interface 클래스명 : 슈퍼클래스명 { ! ! /* 인스턴스 변수 선언 */ } ! ! /* 메소드 선언 */ objC의 클래스 이름은 @end @interface 바로 다음에 나옴 4 11년 9월 7일 수요일
  • 5. @interface 클래스명 : 슈퍼클래스명 { ! ! /* 인스턴스 변수 선언 */ } ! ! /* 메소드 선언 */ 상위클래스는 콜론 다음부모 @end 클래스의 이름으로 명시 5 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 6. @interface 클래스명 : 슈퍼클래스명 { ! ! /* 인스턴스 변수 선언 */ } ! ! /* 메소드 선언 */ 중괄호 사이에 객체 변수 @end (Instance Variable) 선언 6 11년 9월 7일 수요일
  • 7. @interface 클래스명 : 슈퍼클래스명 { ! ! /* 인스턴스 변수 선언 */ } ! ! /* 메소드 선언 */ 메소드는 @interface와 @end @end 사이 & 중괄호 밖에 선언 7 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 8. - (int) age; - (void) setAge: (int) age; + (Person) personWithName: (NSString*) name age: (int) age: - : 인스턴스 메소드 표시 + : 클래스 메소드 표시 8 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 9. - (int) age; - (void) setAge: (int) age; + (Person) personWithName: (NSString*) name age: (int) age: 반환 타입을 괄호 안에 표시 9 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 10. - (int) age; - (void) setAge: (int) age; + (Person) personWithName: (NSString*) name age: (int) age: 메소드 이름 10 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 11. - (int) age; - (void) setAge: (int) age; + (Person) personWithName: (NSString*) name age: (int) age: 콜론(:)은 다음에 인수가 있음 을 표시 11 11년 9월 7일 수요일
  • 12. - (int) age; - (void) setAge: (int) age; + (Person) personWithName: (NSString*) name age: (int) age: 괄호 안에 인수의 유형을 표시 12 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 13. - (int) age; - (void) setAge: (int) age; + (Person) personWithName: (NSString*) name age: (int) age: 인수의 이름 13 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 14. @interface Circle : NSObject { ShapeColor fillColor; ShapeRect bounds; } - (void) setFillColor: (ShapeColor) fillColor; - (void) setBounds: (ShapeRect) bounds; - (void) draw; @end // Circle 14 11년 9월 7일 수요일
  • 15. @implementation 클래스_이름 ! ! /* 메소드 정의 */ @end objC의 클래스 이름은 @implementation 바로 다음에 15 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 16. @implementation 클래스_이름 ! ! /* 메소드 정의 */ @end 메소드는 @implementation와 @end 사이에 정의 됨 16 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 17. @implementation Circle - (void) setFillColor: (ShapeColor) c { fillColor = c; } // setFillColor - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds - (void) draw { NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); } // draw @end // Circle 17 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 18. int main (int argc, const char * argv[]) { ! [객체 메시지_이름: 인수]; ! id shapes[3]; ! ! ShapeRect rect0 = { 0, 0, 10, 30 }; ! shapes[0] = [Circle new]; ! [shapes[0] setBounds: rect0]; ! [shapes[0] setFillColor: kRedColor]; ! ! ShapeRect rect1 = { 30, 40, 50, 60 }; ! shapes[1] = [Rectangle new]; ! [shapes[1] setBounds: rect1]; ! [shapes[1] setFillColor: kGreenColor]; ! ! ShapeRect rect2 = { 15, 19, 37, 29 }; ! shapes[2] = [OblateSphereoid new]; ! [shapes[2] setBounds: rect2]; ! [shapes[2] setFillColor: kBlueColor]; ! ! drawShapes (shapes, 3); ! ! return (0); ! 18 } // main OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 19. ! [shapes[0] setBounds: rect0]; ! [shapes[0] setFillColor: kRedColor]; ! ! [shapes[1] setBounds: rect1]; ! [shapes[1] setFillColor: kGreenColor]; ! ! [shapes[2] setBounds: rect2]; ! [shapes[2] setFillColor: kBlueColor]; ! [객체 메시지_이름: 인수]; 19 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 20. shapes[0] = [Circle new]; [클래스_이름 new]; 20 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 21. ! id shapes[3]; shapes[0] = [Circle new]; ! shapes[1] = [Rectangle new]; ! ! shapes[2] = [OblateSphereoid new]; 21 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 22. void drawShapes (id shapes[], int count) { ! int i; ! ! for (i = 0; i < count; i++) { ! ! id shape = shapes[i]; ! ! [shape draw]; ! } ! } // drawShapes 22 OOP @interface 색션 @implement 색션 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 23. 1. Triangle 클래스 추가하기 (p70) 2. main 함수 수정 (p71) 결과 화면 23 OOP @interface 색션 Shape 확장하기 @implement 색션 MyClass 따라하기 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 24. 1. Application을 생성합니다. - Command Line Tool (Type는 반드시 Foundation으로!) 2. 실행하여 Hello world!가 호출되는지 확인합니다. 3. MyClass라는 클래스를 만들어 봅니다. (Mac OS X - Cocoa - Objectice-C class - Subclass of : NSObject - MyClass를 선택하여 myclass.mm, myclass.h를 생성) 24 OOP @interface 색션 Shape 확장하기 @implement 색션 MyClass 따라하기 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 25. MyClass의 인스턴스를 생성하기 - new, alloc의 방법을 사용 25 OOP @interface 색션 Shape 확장하기 @implement 색션 MyClass 따라하기 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 26. MyClass의 메소드 추가하기. - class method, instance method 모두 추가 26 OOP @interface 색션 Shape 확장하기 @implement 색션 MyClass 따라하기 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 27. MyClass의 메소드 호출하기 - class method, instance method 모두 호출 27 OOP @interface 색션 Shape 확장하기 @implement 색션 MyClass 따라하기 인스턴스 만들기 실습 11년 9월 7일 수요일
  • 28. 1. 추가한 메소드에 매개변수를 넘겨봅시다. - class method, instance method에 매개변수 만들기 - int, float, NSString 등의 여러가지 데이터 타입 이용 28 OOP @interface 색션 Shape 확장하기 @implement 색션 MyClass 따라하기 인스턴스 만들기 실습 11년 9월 7일 수요일