SlideShare a Scribd company logo
1 of 14
Objective-C




                    상속(Inheritance)
                    By Changhoon Park
                    http://changhoonpark.wordpress.com
                                                         Last Update : 2011. 08. 28

11년	 9월	 13일	 화요일
Shape
                                                              fillColor
                                                              bounds
                                                              setFillColor:
                                                              setBounds:
                                                              draw
                   Circle            Rectangle
               fillColor           fillColor
               bounds             bounds
               setFillColor:      setFillColor:      Circle                    Rectangle
               setBounds:         setBounds:
               draw               draw            draw                        draw



                         상속이 없는                          상속을 사용하는 개선된
                    Shapes-Object 아키택쳐                   Shapes-Object 아키택쳐




               2

                         상속    의미
                               문법
                               주요 동작
                               실습



11년	 9월	 13일	 화요일
효율성

                                  코드의 중복

                                 코드 유지의 어려움




               3

                    상속   의미
                         문법
                         주요 동작
                         실습



11년	 9월	 13일	 화요일
@interface Shape : NSObject
                     {
                     ! ShapeColor fillColor;
                     ! ShapeRect   bounds;
                     }

                     - (void) setFillColor: (ShapeColor) fillColor;
                     - (void) setBounds: (ShapeRect) bounds;
                     - (void) draw;

                     @end // Shape




          @interface Circle : Shape                @interface Rectangle : Shape
          {                                        {
          }                                        }

          @end // Circle                           @end // Rectangle




               4

                    상속     의미
                           문법
                           주요 동작
                           실습



11년	 9월	 13일	 화요일
@implementation Circle
         @implementation Shape
                                                 - (void) draw
                                                 {
         - (void) setFillColor: (ShapeColor) c   !   NSLog (@"drawing a circle at (%d %d %d %d) in %@",
         {                                       !   !        bounds.x, bounds.y,
                                                 !   !        bounds.width, bounds.height,
         ! fillColor = c;                        !   !        colorName(fillColor));
         } // setFillColor                       } // draw


         - (void) setBounds: (ShapeRect) b       @end // Circle
         {
         ! bounds = b;
         } // setBounds
                                                 @implementation Rectangle
         - (void) draw                           - (void) draw
         {                                       {
         } // draw                               !   NSLog (@"drawing a rectangle at (%d %d %d %d) in %@",
                                                 !   !        bounds.x, bounds.y,
                                                 !   !        bounds.width, bounds.height,
         @end // Shape                           !   !        colorName(fillColor));
                                                 } // draw

                                                 @end // Rectangle




               5

                     상속     의미
                            문법
                            주요 동작
                            실습



11년	 9월	 13일	 화요일
메소드 디스패칭
                     객체에서 메시지를 받았을 때 메소드 실행
                     현재 객체의 클래스를 찾지 못하면
                     그 객체의 슈퍼 클래스에서 찾는다




               6

                    상속   의미      메소드 디스패칭
                         문법      인스턴스 변수
                         주요 동작   메소드 오버라이딩
                         실습      슈퍼클래스



11년	 9월	 13일	 화요일
- (void) setFillColor: (ShapeColor) c
                                                               {
                                                                         fillColor = c;
                                                               }



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

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




               [shapes setFillColor: kRedColor];




               7

                            상속     의미              메소드 디스패칭
                                   문법              인스턴스 변수
                                   주요 동작           메소드 오버라이딩
                                   실습              슈퍼클래스



11년	 9월	 13일	 화요일
- (void) setFillColor: (ShapeColor) c
                                                   {
                                                             fillColor = c;
                             Shape       code      }
                             class

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


                                                   - (void) draw
                                                   {
                                                   } // draw



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




               8

                        상속   의미       메소드 디스패칭
                             문법       인스턴스 변수
                             주요 동작    메소드 오버라이딩
                             실습       슈퍼클래스



11년	 9월	 13일	 화요일
- (void) setFillColor: (ShapeColor) c
                                                             {
                                                                       fillColor = c;
                                    Shape           code     }
                                     class

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


                                                             - (void) draw
                                                             {
                                                             } // draw



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



                [shapes setFillColor: kRedColor];




               9

                         상속       의미            메소드 디스패칭
                                  문법            인스턴스 변수
                                  주요 동작         메소드 오버라이딩
                                  실습            슈퍼클래스



11년	 9월	 13일	 화요일
인스턴스 변수
                        슈퍼 클래스의 인스턴스 변수를 자신의 인스턴스 변수로 추가

                                                          self
                    @interface RoundedRectangle : Shape            isa       ( NSObject )
                    {
                    ! int radius;
                                                                 fillColor
                    }                                                        ( Shape )
                                                                 bounds
                    @end // RoundedRectangle
                                                                 radius      ( RoundedRectangle )




             10

                       상속    의미        메소드 디스패칭
                             문법        인스턴스 변수
                             주요 동작     메소드 오버라이딩
                             실습        슈퍼클래스



11년	 9월	 13일	 화요일
서브 클래스에서
                     새로운 메소드를 추가
                     기존의 메소드를 수정




             11

                    상속   의미      메소드 디스패칭
                         문법      인스턴스 변수
                         주요 동작   메소드 오버라이딩
                         실습      슈퍼클래스



11년	 9월	 13일	 화요일
- (void) setFillColor: (ShapeColor) c
                                                    {
                                                              fillColor = c;
                              Shape       code      }
                               class

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


                                                    - (void) draw
                                                    {
                                                    } // draw



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




             12

                         상속   의미       메소드 디스패칭
                              문법       인스턴스 변수
                              주요 동작    메소드 오버라이딩
                              실습       슈퍼클래스



11년	 9월	 13일	 화요일
- (void) setFillColor: (ShapeColor) c
                                                                         {
                                         Shape                                  fillColor = c;
                                                            code
                                          class                          }
                                                                         - (void) setBounds: (ShapeRect) b
                                                                         {
                                                                                   bounds = b;
                                                                         } // setBounds


                                                                         - (void) draw
                                                                         {
                                                                         } // draw




                                          Circle            code     - (void) setFillColor: (ShapeColor) c
                                          class
                                                                     {
                                                                         .....
                    0,0,10,30
                    Red Circle                                             [         setFillColor: c];

                                                                     }
                     [shapes setFillColor: kRedColor];




             13


                              상속       의미                메소드 디스패칭
                                       문법                인스턴스 변수
                                       주요 동작             메소드 오버라이딩
                                       실습                슈퍼클래스



11년	 9월	 13일	 화요일
Shape-Object-2 함수를 수정하기
           - 중복되는 부분에 대하여 상속을 이용하여 수정
           - Shape 클래스 만들기
           - p87쪽 참고하여 Circle의 setFillColor 수정하기




             14

                    상속   의미
                         문법
                         주요 동작
                         실습



11년	 9월	 13일	 화요일

More Related Content

More from Hoseo University

More from Hoseo University (10)

Property
PropertyProperty
Property
 
목적이 부여된 에이전트 행동
목적이 부여된 에이전트 행동목적이 부여된 에이전트 행동
목적이 부여된 에이전트 행동
 
실질적인 길 계획하기
실질적인 길 계획하기실질적인 길 계획하기
실질적인 길 계획하기
 
FoundationKit
FoundationKitFoundationKit
FoundationKit
 
Raven
RavenRaven
Raven
 
프로젝트 구성
프로젝트 구성프로젝트 구성
프로젝트 구성
 
구성(Composition)
구성(Composition)구성(Composition)
구성(Composition)
 
Objective-C에서의 OOP
Objective-C에서의 OOPObjective-C에서의 OOP
Objective-C에서의 OOP
 
Dt2210.01.syllabus
Dt2210.01.syllabusDt2210.01.syllabus
Dt2210.01.syllabus
 
Dt3160.01
Dt3160.01Dt3160.01
Dt3160.01
 

상속(Inheritance)

  • 1. Objective-C 상속(Inheritance) By Changhoon Park http://changhoonpark.wordpress.com Last Update : 2011. 08. 28 11년 9월 13일 화요일
  • 2. Shape fillColor bounds setFillColor: setBounds: draw Circle Rectangle fillColor fillColor bounds bounds setFillColor: setFillColor: Circle Rectangle setBounds: setBounds: draw draw draw draw 상속이 없는 상속을 사용하는 개선된 Shapes-Object 아키택쳐 Shapes-Object 아키택쳐 2 상속 의미 문법 주요 동작 실습 11년 9월 13일 화요일
  • 3. 효율성 코드의 중복 코드 유지의 어려움 3 상속 의미 문법 주요 동작 실습 11년 9월 13일 화요일
  • 4. @interface Shape : NSObject { ! ShapeColor fillColor; ! ShapeRect bounds; } - (void) setFillColor: (ShapeColor) fillColor; - (void) setBounds: (ShapeRect) bounds; - (void) draw; @end // Shape @interface Circle : Shape @interface Rectangle : Shape { { } } @end // Circle @end // Rectangle 4 상속 의미 문법 주요 동작 실습 11년 9월 13일 화요일
  • 5. @implementation Circle @implementation Shape - (void) draw { - (void) setFillColor: (ShapeColor) c ! NSLog (@"drawing a circle at (%d %d %d %d) in %@", { ! ! bounds.x, bounds.y, ! ! bounds.width, bounds.height, ! fillColor = c; ! ! colorName(fillColor)); } // setFillColor } // draw - (void) setBounds: (ShapeRect) b @end // Circle { ! bounds = b; } // setBounds @implementation Rectangle - (void) draw - (void) draw { { } // draw ! NSLog (@"drawing a rectangle at (%d %d %d %d) in %@", ! ! bounds.x, bounds.y, ! ! bounds.width, bounds.height, @end // Shape ! ! colorName(fillColor)); } // draw @end // Rectangle 5 상속 의미 문법 주요 동작 실습 11년 9월 13일 화요일
  • 6. 메소드 디스패칭 객체에서 메시지를 받았을 때 메소드 실행 현재 객체의 클래스를 찾지 못하면 그 객체의 슈퍼 클래스에서 찾는다 6 상속 의미 메소드 디스패칭 문법 인스턴스 변수 주요 동작 메소드 오버라이딩 실습 슈퍼클래스 11년 9월 13일 화요일
  • 7. - (void) setFillColor: (ShapeColor) c { fillColor = c; } - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds code Circle - (void) draw class { NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); 0,0,10,30 } // draw Red Circle [shapes setFillColor: kRedColor]; 7 상속 의미 메소드 디스패칭 문법 인스턴스 변수 주요 동작 메소드 오버라이딩 실습 슈퍼클래스 11년 9월 13일 화요일
  • 8. - (void) setFillColor: (ShapeColor) c { fillColor = c; Shape code } class - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds - (void) draw { } // draw - (void) draw Circle code { class NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); 0,0,10,30 } // draw Red Circle 8 상속 의미 메소드 디스패칭 문법 인스턴스 변수 주요 동작 메소드 오버라이딩 실습 슈퍼클래스 11년 9월 13일 화요일
  • 9. - (void) setFillColor: (ShapeColor) c { fillColor = c; Shape code } class - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds - (void) draw { } // draw - (void) draw Circle code { class NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); 0,0,10,30 } // draw Red Circle [shapes setFillColor: kRedColor]; 9 상속 의미 메소드 디스패칭 문법 인스턴스 변수 주요 동작 메소드 오버라이딩 실습 슈퍼클래스 11년 9월 13일 화요일
  • 10. 인스턴스 변수 슈퍼 클래스의 인스턴스 변수를 자신의 인스턴스 변수로 추가 self @interface RoundedRectangle : Shape isa ( NSObject ) { ! int radius; fillColor } ( Shape ) bounds @end // RoundedRectangle radius ( RoundedRectangle ) 10 상속 의미 메소드 디스패칭 문법 인스턴스 변수 주요 동작 메소드 오버라이딩 실습 슈퍼클래스 11년 9월 13일 화요일
  • 11. 서브 클래스에서 새로운 메소드를 추가 기존의 메소드를 수정 11 상속 의미 메소드 디스패칭 문법 인스턴스 변수 주요 동작 메소드 오버라이딩 실습 슈퍼클래스 11년 9월 13일 화요일
  • 12. - (void) setFillColor: (ShapeColor) c { fillColor = c; Shape code } class - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds - (void) draw { } // draw - (void) draw Circle code { class NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); 0,0,10,30 } // draw Red Circle 12 상속 의미 메소드 디스패칭 문법 인스턴스 변수 주요 동작 메소드 오버라이딩 실습 슈퍼클래스 11년 9월 13일 화요일
  • 13. - (void) setFillColor: (ShapeColor) c { Shape fillColor = c; code class } - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds - (void) draw { } // draw Circle code - (void) setFillColor: (ShapeColor) c class { ..... 0,0,10,30 Red Circle [ setFillColor: c]; } [shapes setFillColor: kRedColor]; 13 상속 의미 메소드 디스패칭 문법 인스턴스 변수 주요 동작 메소드 오버라이딩 실습 슈퍼클래스 11년 9월 13일 화요일
  • 14. Shape-Object-2 함수를 수정하기 - 중복되는 부분에 대하여 상속을 이용하여 수정 - Shape 클래스 만들기 - p87쪽 참고하여 Circle의 setFillColor 수정하기 14 상속 의미 문법 주요 동작 실습 11년 9월 13일 화요일