Objective-c on the classes of "2", Objects, and Methods

Source: Internet
Author: User



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


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.