OC basics: partition classification and instance methods

Source: Internet
Author: User

From: http://blog.chinaunix.net/uid-20257772-id-590259.html

This article focuses on the following points:

1. How to create an object-C (OC) Class
2. How to create an OC Class Object
3. Different class methods and instance methods
4. How to define an instance method
5. How to call and execute an instance method

To create an OC class, you need to write two sections: @ interface and @ implementation.

@ Interface is used for class description. The description includes the name and type of instance variables and the method of instance variables. Of course, when describing a method, it indicates its name, whether it has a return value, and whether it uses parameters. For example:

  1. @ Interface fraction: nsobject
  2. {
  3. Int numerator; // defines instance variables.
  4. Int denominator; // defines instance variables
  5. }
  6. -(Void) print; // defines an instance method named print with no return value.
  7. -(Void) setnumerator :( INT) N;
  8. -(Void) setdenominator :( INT) D;
  9. @ End

Fraction is the name of the class we created. fraction: nsobject indicates that fraction inherits the nsobject class.
Int numerator; defines an instance variable and its type is "integer"
-(Void) setnumerator; defines an instance method. "-" indicates "instance method". If it is "+", it indicates the class method, followed by (INT) N; it indicates that the parameter name used by this method is N, and its type is integer. Void indicates that this method has no return value.

So what are class methods and instance methods?

A simple example is the best explanation. Suppose we have a class named car. I need to create a new object: myfirstcar. How can I do this?
Myfirstcar = [Car creatcar];
Then I need to refuel my car. How can I do it?
[Myfirstcar getoil]
Specifically, creatcar is the class method, and getoil is the instance method. It can be seen that the class method can only operate on the "class itself" or be used to create an instance object, while the instance method is to perform operations on the instance.

The class description is complete. We need to fill in the Code for the class description method. If we only describe a name and do not give it the actual content, obviously, it makes no sense.

  1. @ Implementation Fraction
  2. -(Void) print
  3. {
  4. Nslog (@ "% I/% I", numerator, denominator );
  5. }
  6. -(Void) setnumerator :( INT) N
  7. {
  8. Numerator = N;
  9. }
  10. -(Void) setdenominator :( INT) d
  11. {
  12. Denominator = D;
  13. }
  14. @ End

You can see that the function of this class is to print a score. For example, you can define that the numerator is 1 denominator and 2 to print out 1/2.
It makes no sense to explain the entire creation process of the class.

The preceding three methods: Print setnumerator setdenominator, where the content code is detailed.
It is similar to the definition of a function.
@ Implementation is followed by the class name fraction. Do not write an error.

So far, a complete class has been defined. How can we use it?

  1. # Import
    <Foundation/Foundation. h>
  2. Int main
    (INT argc, char
    * Argv [])
    {
  3. NSAID utoreleasepool
    * Pool = [[NSAID utoreleasepool alloc] init];
  4. Fraction * myfraction;
  5. Myfraction = [fraction alloc]; // allocate a bucket
  6. Myfraction = [myfraction init]; // initialize the instance variable myfraction
  7. [Myfraction setnumerator: 1]; // call the instance method
  8. [Myfraction setdominator: 3]; // call the instance method
  9. [Myfraction print]; // call the instance method
  10. [Myfraction release]; // release a bucket
  11. [Pool drain];
  12. Return 0;
  13. }

First, we need to create a class instance myfraction, then allocate the bucket myfraction = [fraction alloc] To myfraction, and then initialize myfraction (including instance variable initialization and instance method initialization) myfraction = [myfraction init];

Then we can use the setnumerator, setdominator, and print methods.
Because no return value is returned, the format is [myfraction setnumerator: 1]; that is, the setnumerator method of myfraction is called, after the setnumerator method is executed, the value of the instance variable numerator is assigned, that is, the value of numerator is set to 1.

To print the final result, we use [myfraction print];

This is the complete process from creation to use of a class.

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.