Objective C Programming Language

Share Embed Donate


Short Description

Download Objective C Programming Language...

Description

c             

               c                     c                 ! "   c  #? R        c      $    c                        %?       &          c      c              #'()* c                             +? j     $        c  ,j    ,               !      #?    j  $   $      c         j  $        c           c      c  -*.         c     %?     $            c         +?       $          c         c!   c                 /?      $   0  0    1  c 1              22     

1 

ÿ?    0 22 1                          3  0   4?        5    1    *!$1       

        &                  22  6?            c 1           &             &                     (?    c 1                0   0  c 1                  7   8               " "0   " "       0 22 9    '? R              3                      1         

  0    #)?   c 1       $       "1"    "1"      *               ##?        c 1                                  &   0       0  1 1         #%?      c 1                    0              0   5       0  :    #+?Î       c 1       0          0                 

#/?   !    c 1          $c 1             0  0               #ÿ?  Î                             &                                   #4?                    5         0  c 1             #6?—           0      $                                        ? ?

?

$ c &      1 1              



      

$ c &      1 1                30            -    c            ; &c 1                        &  c 1           $  

0      c 30                  $  "0             c              $ 0              c    c        #? !        c!        

*         c      $        ,  ,    c   &          c     c             #'()



      

&          c      c             #'() * c                 

          c 1   5     !      #'()" &c 1             1   $ 0             c  0           0c             c            0 c  c         c                    *!$  c    

     0                        > á? á? á?

  c   *  

  cc"       

c           c           c              !    c            0     c       $    >     $          $          "                  $        #   

                         ! á? á? á?

^        !                         !    

$      c      $        c  ,c @,                !   



j     

$        c  ,j    ,                !   

j      $         0            ! 

   >  It is a powerful language,  Easy-to-learn,  Object-oriented version of C,  Provide dynamic binding,  Run-time type identification, and persistence  Easy to understand code  Well organized language

â             ???  ? ? ????  ? ? ?  ?  ? ? ?    ? ? ??? ? ?

? ? ?    ?   ?? ????  ? ? ?    ????? ?  ??? ? ?? ? ??   ? ? ?? ?? ? ?   ???  ?????? ???????? ?  ? ?? ? ?  ?  ?  ? ?? ???  ? ????? ? ? ? ?? ? ? ? ? ? ??? ? ???   ? ? ???   ?? ????? ? ??  ??? ? ? ? ? ? ? ????   ?? ?? ??   ?  ?  ? ? ??? ???????? ? ? ?  ? ????? ? ??? ? ?   ?  ?  ? ?? ? ? ? ? ?

  ? ? ?? ?  ? ?

  !        "? ? ???? ? ? ? ?  ?  ?  ? ?? ?  ? ?  ??  ?? ? ???? ?  ? ? ??   ? ?  ? ? ? ? ? ? ?  ? ??  ? ?? ?   ?  ?   ? ? ???????? ?  ?? ? ? ?  ?   ? $   $      c     



   j  

$   $      c     $  $        c                $  %   &           A linclude int main(void) { printf("Hello, World!"); return ; }

!       " "    >?  c  c!          $           -*.                -*.         c     $        c          c       



   j  

$        c       

  c      c  -*.          c           j 

&-*.                       c   -*.                 &              >??   ?  ?   A            ! #? Î! -    >??   ?  ?      -*.       $        1  1 )#'%1  &  0     1  1)#'%1            %? !    -   B>C-*.  CC CD  +? c   1;   1;-*.  1;   &  0        

      A      -*.       c          $             c        $            c       

   



$            c             &      —  &'

 &             =      > ?  ? ? ? ?  ?? ?? ? ? ? ? ? ?

]   () *&*)  *A  *A    *A 

  1            " &        ] *   # c    %))(1)#1%4%+>#)>+%'(+ 7+(#>#) 8  A

  $          c        c! 



      

$          c         c!   c                      -                       ]!*    

 &           ]   

  &         B " "D &      ] *  



       E

 ? ?

?

    0  0    1  



     

O? ?  ?  ?? ? ?  ? ??? ?  ?  ?  ? ??  ??? !???  ? ?? ?""?? ??   ? ? ??   ?!? ? ?   ? ?? ? ? ? ??#$%? ? ?  ??  ?&???? ?'   ? ?

Î    +  ! Î  

Î            

,  

,                     ,             

, 

Î          :  F  Î  

Î   

, 

               

, 

         

   

,  

G                 

       Î   , 

Î    :    0      

, 

&      

, 

       F   0

, 

  0              F   0

Î         Î  

Î   

,

:          

, - ./

$           H 

G     H    B         DBF     , - ./

    B   H D       D ,-./

                 H 

,0 0

:     *!! 

  

   Î   ** 5  

Î    &        0 22       "  



 0 22 1              

  



   

0 22 1                          3  0                   3  0    > 6   7  G                 B   D I  B   D I   B            D &             

 0 22  ?? ?J1111J?    =   > #? ??  %? ?J   J? ? 5    1    *!$1             

  



       

[??  ? ? ?    ??? ? ?? ?  ?  ??? ? ? ?   ?&?? ?? ? ?? ?  ??  ? ? ? ?  ? ?? ?"" ? &??? ??  ??$ ?   ? ?

Î   !  limport"SuperClass.h" limport @interface limport ClassName:SuperClass { @interface MyClass:NSObject{ variable int a; daclaration; int b; variable } daclaration; -(void) setvara : (int) x; } -(void) setvarb : (int) y; method -(int) add; declaration; @end method declaration; @end

Î    !  limport limport"MyClass.h" @implementation MyClass -(void) setvara :(int) x{ a=x; } -(void) setvarb :(int) y{ b=y; } -(int) add{ return a+b; } @end

     limport limport"MyClass.m" int main(){ MyClass *class = [[MyClass alloc]init]; [class setvara : 5]; [class setvarb : 6];

printf("Sum is : %d",[class add]); [class release]; return ; }



            ?  ?? ?? ? ? ?  ?&? ? ? ?? ? ???  ?  ? & ? ????  ???? ?  ??  ?

   limport @interface MyClass:NSObject{ } // declare method for more than one parameter -(int) sum: (int) a andb: (int) b andc:(int)c; @end

    limport limport"MyClass.h" @implementation MyClass -(int) sum: (int) a andb: (int) b andc:(int)c;{ return a+b+c; } @end

    limport limport"MyClass.m" int main(){ MyClass *class = [[MyClass alloc]init]; printf("Sum is : %d",[class sum : 5 andb : 6 andc:10]); [class release]; return ; }

 ! & !

" ?

        ?  ?? ? ?   ? ? ???? ?? ?( ??  ?? ?? ?? ?? ?   ? ?   ?)? *!?  ?    ?  ??   ?? ?? ??  ? ? ?++?!? ?++?? ???  ?? ?""? ?, ?&? ?   ? ?- .? ?   ? -.? ?? ?? ?   ??/0? ? ?  ?  ? ??  ?? ?  ? ?? ?

!    limport @interface MyClass:NSObject{ int a; int b; } // declare constructor -(MyClass*) set:(int) a andb:(int) b; -(void) sum; @end

  limport limport"MyClass.h" @implementation MyClass // define constructor -(MyClass*) set:(int) x andb:(int) y { self = [super init]; if(self) { a=x; b=y; return self; } } -(void) sum { printf("Sum is : %d",a+b); } @end

   limport limport"MyClass.m" int main(){

// use constructor MyClass *class = [[MyClass alloc] set : 10 andb : 12]; [class sum]; [class release]; return ; }

! ! > %% ?

? R               3                     1            0  5   3   0          -        %)              &               c 1" 

  22    

R—    :    1 F  " 022 1            # F             !    limport @interface MyClass:NSObject { @private int a; int b; } -(void) set:(int) x andb:(int) y; -(void) sum; -(void)show; @end

  

limport limport"MyClass.h" @implementation MyClass -(void) set:(int) x andb:(int) y { a=x; b=y; } -(void) sum { printf("Sum is : %d \n",a+b); } -(void)show{ printf("value of a is : %d \n",a); printf("value of b is : %d \n",b); } @end

    limport limport"MyClass.m" int main(){ MyClass *class1 = [[MyClass alloc] init]; MyClass *class2 = [[MyClass alloc] init]; [class1 set: 10 andb :12]; [class1 show]; [class1 sum]; // This is invalid statement because variable a is private. // class2->a = 10; class2->b = 15; [class2 show]; [class2 sum]; [class1 release]; [class1 release]; return ; }

! value of : 10 value of : 12 Sum is : value of : 0 value of : 15 Sum is :

?

a is b is 22 a is b is 15

  ?

c 1       $       "1"    "1"      *                

 

 

limport limport"MyClass.h" @implementation MyClass limport -(void)instanceShow { @interface MyClass:NSObject { printf("This is instance level method.\n"); } } -(void)instanceShow; +(void)classShow { +(void)classShow; printf("This is class @end level method."); } @end

  

limport limport"MyClass.m" int main(){ MyClass *instance = [[MyClass alloc] init]; [instance instanceShow]; [MyClass classShow]; [instance release]; return ; }

!   &      &    

           " ! "                  ! BD          2B D    1                       X  X?

        ?

c 1                                   &  0       0  1 1         &                1  , !  0          " , !  0           0      *!^     # , !    0             8 , !                    &        &                ,    K

 A  !A        X  X?

      c 1                    0              0  5  

    0  :                  0        L       50                   L   



 ! 

&       A   A   

 M  ,=  ,

F    =   1 limport @interface FirstClass:NSObject B D *# >BDN { } #OP int num1;  -(void)setNum1 :(int) x; B,# > -(int)getNum1; QC,#DP @end R  1BD*# N    #P R F &        &  &  



limport

limport "SecondClass.h"

"FirstClass.m" @interface SecondClass: FirstClass { int num2 ; } -(void)setNum2 :(int) y; -(int)mul; @end

limport "FirstClass.h" @implementation SecondClass -(id) init { self = [super init]; return self; } -(void)setNum2 :(int) y { num2 = y ; printf("num2 is : %d \n", num2); } -(int)mul { return num2*[self getNum1]; } @end

&           limport "SecondClass.m" limport int main() { FirstClass *obj1 = [[FirstClass alloc] init]; SecondClass *obj2 = [[SecondClass alloc] init]; [obj1 setNum1 : 10 ]; [obj2 setNum2 : 15 ]; printf("Multiplication Result : %d \n",[obj2 mul]); return 0; }

! num1 is : 10 num2 is : 15 Multiplication Result : 150

X  X?

Î       c 1       0          0                &                    

  -9)/   !  

Î             c 

-9)/    !        c            -9)/

 & !                :-9)/ ; & !             - /  & !  

 0    



 !   A   A   



limport "FirstClass.h" @implementation FirstClass limport -(void)fShow @interface FirstClass:NSObject { -(void)fShow ; printf("This +(void)classShow ; is first @end class."); }



+(void)classShow { } @end

 &  &  



limport "SecondClass.h" @implementation limport SecondClass @interface -(void)sShow { SecondClass:NSObject printf("This -(void)sShow; is first @end class."); } @end   



limport "FirstClass.m" limport "SecondClass.m" limport int main() { FirstClass *fClassObj = [[FirstClass alloc] init]; SecondClass *sClassObj = [[SecondClass alloc] init]; /* some methods to work with dynamic types */ // -(BOOL) isKindOfClass: classObj ---------- true if ( [fClassObj isKindOfClass: [FirstClass class]] == YES ) { printf( "fClassObj is kind of FirstClass.\n" ); } // -(BOOL) isKindOfClass: classObj ---------- false if ( [fClassObj isKindOfClass: [SecondClass class]] == YES ) { printf( "fClassObj is kind of SecondClass.\n" ); } else printf( "fClassObj is not kind of SecondClass.\n" ); // -(BOOL) isMemberOfClass: classObj ---------- true if ( [fClassObj isMemberOfClass: [FirstClass class]] == YES ) { printf( "fClassObj is member of FirstClass.\n" ); } // -(BOOL) isMemberOfClass: classObj ---------- false if ( [sClassObj isMemberOfClass: [FirstClass class]] == YES ) { printf( "sClassObj is member of FirstClass.\n" ); } else printf( "sClassObj is not member of FirstClass.\n" ); // -(BOOL) respondsToSelector: selector ----- true if ( [fClassObj respondsToSelector: @selector(fShow)] == YES ) { printf( "fClassObj responds to fShow method\n" ); } // -(BOOL) respondsToSelector: selector ----- false if ( [sClassObj respondsToSelector: @selector(fShow)] == YES ) {

printf( "sClassObj responds to fShow method\n" ); } else printf( "sClassObj does'nt respond to fShow method\n" ); // release memory allocated for the objects [fClassObj release]; [sClassObj release]; return 0; }

! fClassObj is kind of FirstClass. fClassObj is not kind of SecondClass. fClassObj is member of FirstClass. sClassObj is not member of FirstClass. fClassObj responds to fShow method sClassObj does'nt respond to fShow method

X  X? $           c 1                              

 @

    !    

$           c 1                                 @      =     0                c 1          $c 1             0  0            7   8     # 7   8     #        myClassObj1 retain count is : 1 myClassObj2 retain count is : 1 myClassObj1 retain count is : 3 myClassObj2 retain count is : 2 myClassObj1 retain count is : 1 myClassObj2 retain count is : 1

X  X?                             



  Î 

?

                        

   &                                  $                      1      &                  *!!       !                        ! &  limport limport @interface Student: NSObject { NSString *fName; NSString *lName; NSString *email; } -(void) set: (NSString*) f last: (NSString*) l email: (NSString*) e; -(void) setFirst: (NSString*) f; -(void) setLast: (NSString*) l; -(void) setEmail: (NSString*) e; -(NSString*) firstName; -(NSString*) lastName; -(NSString*) email; -(void) print;

@end

&  limport "Student.h" limport @implementation Student -(void) set: (NSString*) f last: (NSString*) l email: (NSString*) e { [self setFirst: f]; [self setLast: l]; [self setEmail: e]; } -(NSString*) firstName { return fName; } -(NSString*) lastName { return lName; } -(NSString*) email { return email; } -(void) setFirst: (NSString*) f { [f retain]; [fName release]; fName = f; } -(void) setLast: (NSString*) l { [l retain]; [lName release]; lName = l; } -(void) setEmail: (NSString*) e { [e retain]; [email release]; email = e; } -(void) print { printf( "%s %s %s", [fName cString],[lName cString],[email cString] ); } -(void) dealloc { [fName release]; [lName release]; [email release]; [super dealloc]; } @end

   limport "Student.m" limport limport int main( int argc, const char *argv[] ) { NSString *fName =[[NSString alloc] initWithCString: "Mahendra"];

NSString *lName = [[NSString alloc] initWithCString: "Singh"]; NSString *email = [[NSString alloc] initWithCString: "[email protected]"]; Student *mahendra = [[Student alloc] init]; [mahendra set: fName last: lName email: email]; // first release string objects [fName release]; [lName release]; [email release]; // show the retain count printf( "Retain count: %i\n", [mahendra retainCount]); [mahendra print]; printf( "\n" ); // free memory [mahendra release]; return 0; }

X  X?                 



  

                 5         0  c 1                                  $       5            !          5  $    

          ! &         9  

9  

limport M ,5  , @interface BaseClass :  NSObject { F    int num1, num2; 5   } -(void)set :(int) x and: 1B D >BD > (int) y; BDN -(int)add; #OP -(int)sub; %OP @end



R 1BD N   #2%P R

1BD  N B#;%DN   #1%P R     %1#P R F &                  &  

&  

limport"BaseClass.h" @interface BaseClass(Category) -(void)show:(int)x; @end

limport"SubClass.h" @implementation  BaseClass(BaseClass)  -(void)show:(int)x { printf("Result is :  %d \n",x); }  @end





  



 limport"BaseClass.m" limport"SubClass.m" limport int main(){ BaseClass *obj = [[BaseClass alloc] init]; [obj set:10 and:8]; [obj show:[obj add]]; [obj show:[obj sub]]; [obj release]; return 0; }

! Result is : 18 Result is : 2

X  X?          0       



—  

         0      $                                        $         5   !     BD      5    

                               ! 9  

9  

limport limport"BaseClass.h" @interface BaseClass : @implementation BaseClass NSObject { -(void)set :(int) x int num1, num2; and: (int) y { } num1 = x; -(void)set :(int) x and: num2 = y; (int) y; } -(int)add; -(int)add { -(int)sub; return num1+num2; -(void)show:(int)x; } @end -(int)sub { if(num1>num2){  return num1num2; } else return num2num1; } -(void)show:(int)x { printf("Base class result : %d \n",x); } @end

             

&  

&  



limport"BaseClass.h" @interface SubClass : BaseClass -(void)show:(int)x; @end

limport"SubClass.h" @implementation SubClass -(void)show:(int)x { printf("Sub class result : %d \n",x); } @end





limport"BaseClass.m" limport"SubClass.m" limport int main(){ // create base class object. BaseClass *obj1 = [[BaseClass alloc] init]; [obj1 set:10 and:8]; [obj1 show:[obj1 add]]; [obj1 show:[obj1 sub]]; // pose subclass to baseclass. [SubClass poseAsClass: [BaseClass class]]; // create base class object after posing. BaseClass *obj2 = [[BaseClass alloc] init]; [obj2 set:11 and:6]; [obj2 show:[obj2 add]]; [obj2 show:[obj2 sub]]; // free memory

   

[obj1 release]; [obj2 release]; return 0; }

! Base class result : 18 Base class result : 2 Sub class result : 17 Sub class result : 5

?

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF