Four. OC Foundation--1. Document installation and method overloading, 2.self and super&static,3. Inheritance and derivation, 4. Instance variable modifier, 5. Private variable & private method, 6.description method

Source: Internet
Author: User

Four. OC Foundation--1. Document installation and method overloading,

1. Online Installation
xcode-> System Preferences->downloads->doucument-> Download

2. Offline installation
Baidu Xcode Documentation

3. Method overloading: Refers to defining multiple methods with the same name in a class
No overloads in OC

    

2.self and Super&static,

Self and Super:
1. Self understands: Who calls the current method, self represents who.
For example: In an object method, self represents an object, because only objects can call object methods
In a class method, self represents a class, because only the class name can call a class method.

Note: Do not use self to call class methods in Object methods
Do not use the self Call object method in a class method

2. Gets the class of the current object: Person *p = [person P];
[P class] = [Person class] = person

3. Member variables can be accessed through Self->_speed in the object method.

4. Super represents the parent class of the current class.

Static keyword:
1. Static in the C language can modify local variables, global variables, functions
2. In OC, cannot be used to modify instance variables and methods, but can be decorated with local variables, global variables

3. Inheritance and derivation,

Inheritance format:
@interface Cat:animal

@end

Inheritance definition: Subclasses have properties and methods.

Derivation: The process by which a parent class is produced down a subclass is called a derivation

Considerations for Inheritance:
1. Subclasses cannot define variables with the same name as the parent class, but can inherit the variables of the parent class
2. OC class supports single inheritance and does not support multiple inheritance

3. We all know that Objective C cannot support multiple inheritance like C + +, but the use of OC often encounters situations where multiple inheritance is required. For example, there are methodb in Methoda,classb in ClassA,

And now you need to use the methods in these two classes.

Answer: Multiple inheritance in OC can be achieved through protocol

4. Instance variable modifiers,

Scope of Action:
@public: Acting on all classes
@protected: Acting on the current class, subclass (derived class)
@private: Acting on the current class
@package: Action Frame level

By default, instance variables belong to the @protected adornment.

5. Private variables & private methods,

Define the format:
Defined in. m files
@implementation Person:nsobject
{
int age; Private variables
}

Not declared in the. h file, only implemented in. m files
-(void) run{

NSLog (@ "I am a private method");
}
@end

Private methods can only be used in the current file and cannot be inherited by the quilt class.

6.description method

1. The description method returns the descriptive information for the object by default (the default implementation is to return the class name and the memory address of the object);

2. Rewrite the description method:
-(NSString *) description{
return [NSString stringwithformat:@ "Number of dog legs:%d, number of eyes of dog:%d", _tuinum, _eyenum];
}

3. Never use%@ and self in the description method at the same time
-(NSString *) Description {
return [NSString stringwithformat:@ "%@", self];
}
%@ and self are used at the same time to represent the description method that calls self, which eventually causes the program to go into a dead loop, calling the description method

Four. OC Foundation--1. Document installation and method overloading, 2.self and super&static,3. Inheritance and derivation, 4. Instance variable modifier, 5. Private variable & private method, 6.description method

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.