Objective-C object-oriented initial experience

Source: Internet
Author: User

1. Class Declaration

1-1 @ interface start and @ end

1-2 variables and methods cannot be mixed together, and instance variables must be placed in {}

1-3 no static or const instance variables. In the. M file, you can declare the static property: it belongs to a class and does not belong to an object. Similarly, there is no const method, and no youyuan method.

1-4 The "-" method is protect, and the "+" method is public, if a method is in. H is not declared, but in. m can only be defined. the private method used inside the class in the M file. All methods cannot be implemented in the class declaration.

1-5 All classes are inherited from nsobject.

From 1 to 6, the default access permission for instance variables is @ protected.

The Return Value Type and parameter of the 1-7 method can be left empty. The default value is ID (equivalent to void * generic pointer type in C ++ ).

From 1 to 8, methods in the same class cannot be reloaded, that is, the signatures of methods cannot be exactly the same.

 

2. Class implementation

2-1 and the. h file must have a method implementation body to ensure the same signature. If a method is not declared in. h but is defined in. m, it can only be the private method used inside the class in the. M file.

 

3. Class instantiation

3-1 in objc, all objects are created in the heap, and objects cannot be created in the stack. For example, allocate memory in the heap area and create an object.

Fraction * frac = [fraction alloc] init];

3-2 about Null Object nil. Similar to null in C ++.

Difference: Nil is an object, and null is only a value of null = 0

Nil callers and methods do not work, and crash is not generated or an exception is thrown.

The 3-3-alloc method inherits from nsobject, And the subclass cannot be overloaded (The subclass can override the init method ). Alloc is a class method and can only be called by class name.

3-4 after alloc allocates memory, you also need to use the init method to initialize the instance variables.

 

4. Messages

The biggest feature of objc is message transmission.

The message receiver must be an object. The real parameter of the message recipient can be a message expression.

 

5. object initialization

5-1 after the object is created, the object's memory is initialized to 0.

5-2 Implementation of the initialization method (mode ):

-(ID) Init

{

If (Self = [Super init])

{

...

}

Return self;

}

5-3 there can be multiple initialization methods. Generally, all initialization Methods start with init. Initialization methods with few parameters can indirectly call multiple initialization methods with multiple initialization parameters for reuse.

5-4 A class can have multiple initialization methods, but only one of them can be specified. It generally has the most parameters and the initialization method that does the most work. You can call other initialization methods.

 

6. Attributes

Class 6-1 instance variables are generally protected or private. They cannot be accessed through objects and can be accessed through get or set. Get and set naming rules: Get + instance variable names (uppercase letters). Set is the same.

6-2 attribute declaration mechanism:

@ Property type name attribute name written in. h

@ Synthesize attribute name written in. m

@ Property int age; the default value is readable and writable readwrite, that is, the get and set methods are automatically generated. @ Property (readonly) intage; read-only attribute. Only get methods are generated. @ Property (writeonly) int age; only attributes can be written. Only the set method is generated.

6-3 attribute access

Stu. Age; = [STU age]; call the get method.

Stu. Age = 10; = [STU setage: 10]; both call the set method.

 

7. Self and super

7-1 self is a hidden parameter of the method, similar to this in C ++,

In the. M file, self is a pointer to the current instance, and its value can be changed. (Self-> age)

Self cannot be omitted when one method calls another method of the same type.

([Self initwithage: 20])

You must use self to specify the message recipient.

7-2 super is a compilation indicator. It only tells the compiler that when calling the class method, the method of the parent class should be called, rather than the class, but the message receiver is still the class, that is, super does not change the message receiver.

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.