Class Definition in objective-C

Source: Internet
Author: User

As I writeProgramThe experience is not quite rich, and the understanding of object-oriented is still stuck in the state of knowing the principle but not how to use it efficiently, so here is a brief introduction to my understanding. object-oriented programming is like a profound and profound Chinese culture, and we still need to continue to study, practice, and practice.

 
 

Class Definition

The OC class is divided into two files: A. H file and a. m file.

. H file storage class, function declaration, class declaration with the keyword @ interface @ end to declare

The specific implementation of the. M file storage class is implemented using the keyword @ implementation @ end.

Object methods and class methods

To declare and implement a class function, you must use + or-to start the function.

+ Represents a class method, which is equivalent to a static function in Java.

-Indicates the object method, which can be called only after the object is instantiated.

 
Class Declaration: # import <Foundation/Foundation. h> @ interface Student: nsobject {}@ end class implementation: # import "student. H" @ implementation dog @ end


relationship between classes and objects
In general, object-oriented programming includes the following sentence: everything is an object. Of course, to do this in the object-oriented programming process, you need a wealth of Code coding experience.
what is an object? For example, a person, a car, or even a wheel on the car, and screws on the wheel can be treated as an object, the key is how you deal with it in the programming process. Finally, let's say, everything is an object.
what is a class: class is to extract the common content of an object and abstract the specific content. For example, for a class, we can extract the common content of the class into a student class, which includes student IDs, ages, and classes. In oC, it is defined by @ interface. The thing implemented by @ implementation is a class.
objects in OC are declared by pointers, such as student * Xiaoming;
Create an object in OC and use alloc to create an object, the compiler will allocate an available memory to the Xiaoming object in the heap memory. Then you need to initialize the object to call the init method, so that this object can be used, such as: Student * Xiaoming = [STUDENT alloc] init];
each object of the same class has a storage space for different instance variables.
each object of the same class shares the method of this class.
Implementation of the initialization method:
-(ID) Init
{< br> Self = [Super init];
If (Self)
{
// init Code
}< br> return self;
}

It is generally written as follows:
-(ID) Init
{
If (Self = [Super init];)
{
// Init code
}
Return self;
}

Self refers to the class object itself, and super refers to the parent class object.
The initialization method of the subclass must call the specified Initialization Method of the parent class, and initialize the parent class by sending the corresponding message to super.
[Self calss]; // obtain the name of this class
[Super class]; // get the name of the parent class

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.