Classes, Objects, and Methods
Learn about the classes, objects, and methods in Object-c below.
Objective-c as an object-oriented language, the most basic is the name of Object.object, is the object, that is, one thing.
In the case of real life, our car is an object, it has its own properties (color, brand, etc.), its own method (start, turn around, etc.).
In OC, if we define an object as Mycar, when we need to use its start and turn methods, use the following:
mycar.start ();
  |
parameterless method |
with parameter method |
objective-c notation |
< Span style= "FONT-SIZE:16PX;" >[mycar start]; |
[mycar Start:left]; |
java notation |
mycar.turn (left); |
There is a way to invoke the object method of appeal, but we also lack the definition of the object method. How is a class defined in Objective-c?
Need to pass two key @interface and @implementation sections
interface and implementation collectively represent a class, and the combination of the two is equivalent to class in Java.
Here's a description of a class that constructs fractions:
The @interface section is equivalent to the interface section, which lists the methods (print, setnumerator,setdenominator) that will be used in the class--- make a stencil
-(void) print; The method in OC starts with "-", indicating that this method, immediately following (void), represents the return value, and finally the name of the method, print.
-(void) Setnumerator: (int) n; When you see the method using ":", it indicates that the method is with parameters. The (int) parameter type is shaping, and parameter names are called N.
1 @interface Fraction: NSObject
2 -(void) print;
3 -(void) setNumerator: (int) n;
4 -(void) setDenominator: (int) d;
5 @end
The @implementation part is equivalent to the implementation part, overriding all the methods of the interface-- mold as a template, concrete implementation
1 @implementation Fraction
2 {
3 int numerator;
4 int denominator;
5 }
6 –(void) print
7 {
8 NSLog (@"%i/%i", numerator, denominator);
9 }
10 –(void) setNumerator: (int) n
11 {
12 numerator = n;
13 }
14 –(void) setDenominator: (int) d
15 {
16 denominator = d;
17 }
That's how OC passes.
[Email protected]
[Email protected]
3.[mycar start];
The method was created, and the call was completed.
The above main reference programming in OBJECTIVE-C 6th Edition content, entered the personal study, if have errors and omissions, please correct me.
Objective-c on the classes of "2", Objects, and Methods