Objective-C in my eyes

Source: Internet
Author: User

From the first applicationProgramStart with helloworld:

Objective-C version:

 # Import  <  Foundation  /  Foundation. h  >  

Int Main ( Int Argc, Const Char * Argv []) {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

// Insert code here...
Nslog ( @" Hello, world! " );
[Pool drain];
Return 0 ;
}

# Import <Foundation/Foundation. h> indicates to import or include information about the system file Foundation. H into a program.

Int main (INT argc, const char * argv [])

This function is a special function used to accurately indicate where the program will start execution. This function returns the int type.

NSAID utoreleasepool*Pool=[[NSAID utoreleasepool alloc] init];

This statement reserves the memory space for the automatically released pool.
Nslog is a function in the objective-C library.
[Pool drain] releases the allocated memory pool.

C # version of the hello World Program:

UsingSystem;
NamespaceFirstapp
{
ClassProgram
{
Static VoidMain (String[] ARGs)
{
Console. writeline ("Hello World");
}
}
}

Let's take a look at a complete console application:

Objective-C version:

 # Import  < Foundation  /  Foundation. h  >  

@ Interface fraction: nsobject
{
Int Numerator;
}

- ( Void ) Print;
- ( Void ) Setnumerator :( Int ) N;

@ End

@ Implementation Fraction

- ( Void ) Print
{
Nslog ( @" % I " , Numerator );
}
- ( Void ) Setnumerator :( Int ) N
{
Numerator = N;
}

@ End

Int Main ( Int Argc, Const Char * Argv []) {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

Fraction * Fraction = [Fraction alloc];
Fraction = [Fraction init];

[Fraction setnumerator: 10 ];

Nslog ( @" The value of numerator is: " );
[Fraction print];

[Fraction release];
[Pool drain];
Return 0 ;
}

We willCodeTranslate to C #:

 Using  System;

Namespace Firstapp
{
Public Class Fraction: Object
{
Private Int Numerator;
Public Void Setnumberator ( Int N)
{
Numerator = N;
}
Public Void Print ()
{
System. Console. writeline (numerator );
}

}
Class Program
{
Static Int Main ( String [] ARGs)
{
Fraction = New Fraction ();
Fraction. setnumberator ( 10 );
Console. writeline ( @" The value of numerator is: " );
Fraction. Print ();
Return 0 ;
}
}
}

In objective-C, the base class of all classes is nsobject, while in C #, the base class is object.

In objective-C,

@ Interface Fraction

@ End

@ Interface is used to describe classes, data members of classes, and methods of classes.

In C #, it indicates:

Class fraction {

}

In objective-C,

@ Implementation is used to implement the methods of classes defined in @ interface.

In objective-C,

IntNumerator; here, numerator is an instance variable, defined in {} after the class name.

-(Void) Print; the class method print is defined here, which indicates the class instance method.

If you define+(Void) Print; indicates the class method.

In C,

Static void print (){

}

Indicates the class method.

In objective-C,

-(Void) Setnumerator :(Int) N is the parameter of the setnumerator method, which is of the int type.

In C,

Void setnumerator (int n ){

In objective-C,

-(Void) Setto :(Int) N over :(Int) D;

N and D are parameters. This is a multi-parameter function.

There is also a representation:

-(Void)Set:(Int) N :(Int) D {
Numerator=N; numerator1=D;
}

In C,

Void setto (int n, int d ){

}

 

In objective-C,

@ PropertyIntNumerator1;

@ Synthesize numerator1;

Is the abbreviated form of an attribute.

In C #, it is equivalent:

Public int numerator1 {Get; set ;}

A complete simple example of objective-C object-oriented:

 # Import  <  Foundation  /  Foundation. h  > 

@ Interface fraction: nsobject
{
Int Numerator;
}



- ( Void ) Print;


- ( Void ) Setnumerator :( Int ) N;
- ( Int ) Numerator;
- ( Void ) Setto :( Int ) N over :( Int ) D;

@ Property Int Numerator1;


@ End

@ Implementation Fraction

- ( Void ) Print
{
Nslog ( @" % I " , Numerator );
}

- ( Void ) Setnumerator :( Int ) N
{
Numerator = N;
}

- ( Int ) Numerator
{
Return Numerator;
}

@ Synthesize numerator1;

- ( Void ) Setto :( Int ) N over :( Int ) D {
Numerator = N; numerator1 = D;
}

- ( Void ) Set :( Int ) N :( Int ) D {
Numerator = N; numerator1 = D;
}

@ End

Int Main ( Int Argc, Const Char * Argv []) {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

Fraction * Fraction = [Fraction alloc];
Fraction = [Fraction init];

[Fraction setnumerator: 100 ];
[Fraction setnumerator1: 99 ];


Nslog ( @" The value of numerator is: " );
[Fraction print];

Nslog ( @" The value of numerator is: % I " , [Fraction numerator]);

Nslog ( @" The value of numerators is: % I " , [Fraction numerator1]);

[Fraction setto: 8 Over: 9 ];

Nslog ( @" The value of numerators is: % I, % I " , [Fraction numerator], [fraction numerator1]);

[Fraction Set : 2 : 3 ];

Nslog ( @" The value of numerators is: % I, % I " , [Fraction numerator], [fraction numerator1]);


[Fraction release];
[Pool drain];
Return 0 ;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.