Implementation of Basic Level 2 for iphone Development

Source: Internet
Author: User

Then the class file in Objective c uses the extension m.

[Plain]
Fraction. m
# Import "Fraction. h"
@ Implementation Fraction
-(Void) setNumerator: (int) n {
Numerator = n;
-(Void) setDenominator: (int) d {
Denominator = d;
}
-(Void) setNumerator: (int) n andDenominator: (int) d {
Numerator = n;
Denominator = d;
}
-(Int) numerator {
Return numerator;
}
-(Int) denominator {
Return denominator;
}
-(Void) print {
Printf ("% d/% d \ n", numerator, denominator );
}
-(Void) m {
Printf ("-m: The class variable t is % d \ n", ++ t );
}
+ (Void) t {
Printf ("+ t: The class variable t is % d \ n", ++ t );
}
@ End

Because we put Fraction. m and Fraction. h in a folder, # import uses "".
The task is the method in the implementation interface, so the difference with the interface structure is that you cannot define variables here,
@ Interface is replaced with @ implementation, so there is nothing special about the rest. You do not need to implement @ interface here
All methods in, which will not cause errors. Here we have a-(void) m method that is quite special.
Can this method be called? Because Object-C is a dynamic language, even if it is not specified in @ interface
Can still be called.
In addition, note that the difference between the setter method and the interface is that the parameter names are abbreviated to n and d, because
Local variables (variables defined in parameters and methods) are hidden when names conflict.
As a result, numerator = numerator becomes meaningless. Of course, you can use the self key mentioned later
It is written as self-> numerator = numerator, which is a common method of writing this. x = x in JAVA.
Next we will write the call code. Because Object-C is based on the C language, the program entry is still the main function. This
Note # import is h, not m. The Code is as follows:

Main. m

[Html]
# Import "Fraction. h"
Int main (int argc, const char * argv []) {
Fraction * frac = [[Fraction alloc] init];
[Frac setNumerator: 3 andDenominator: 5];
[Frac print];
Printf ("The denominator of Fraction is % d \ n", frac-> denominator );
[Fraction t]; // call class methods
[Frac m];
[Frac release];
Return 0;
}

(1) In the first line, we created the Fraction instance (Object). In Object-C, the instance can only use pointers as variables
The value cannot be used, so you see * frac, rather than frac, which is consistent with JAVA.
The variables in the example (referred to as references in JAVA) are also pointers, but JAVA does not have the pointer concept, so you do not
See *. As for the code for creating an instance on the right of the equal sign, you can see it below. Ignore it here.
(2) The second line of code calls the method that sets two variables at the same time. We can see that the syntax format of the Object-C call method is
[Class or instance pointer Method Name: parameter 1 tag 1: parameter 2... ...]. This call format is called infix.
Syntax. It looks a bit strange for the first time, but it is more effective. For example, you took over a resigned employee.
Program, the JAVA program calls a method with five or more parameters, but you do not have this
Method APIs, it is hard to guess what these five parameters are for, but Object-C calls
You must have a method label before each parameter, so that you can easily see this from the label name
What does a parameter mean.
(3) The fourth row uses the syntax of Object> member variables in the printf () function of C to access the variables of the instance.
Instead of using the getter method. You cannot access the numerator variable because it is
@ Protected can only be accessed directly by this class or subclass.
(4.) We call the Class Method t on the fifth line. You can also use the following method:
[[Fraction class] t];
Or
Class clazz = [Fraction class];
[Clazz t];
The class method is a class method from NSObject, which is equivalent to the getClass () method in JAVA.
Take the Class object of this Class, there is no * Before clazz *. You must also get used to this nested call method.
It is no different from A. B (). c () in JAVA.

(5) In the sixth line, we call the m method. You will find that the method is not declared in @ interface.
Yes, but a warning is received during compilation. This is why all the previous objects-C are dynamic languages.
But in general, you will provide h files to others, so the h files you write in m files do not have
The method is not known by others. This method is equivalent to private in disguise.

(6) In row 7, we released the memory space applied for by the frac instance in alloc of the first line, after the memory management of Object-C.
. In addition, you will find that the alloc, init, and release methods are not defined in Fraction. h.
Called. Obviously, these methods come from the parent class NSObject.


From TQUDING's column

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.