Class methods and Object methods for iOS development OC

Source: Internet
Author: User

First, let's take a look at what class methods and object methods are.

Object method:

The object is preceded by "-". such as:-(void) Runner;

Object methods can only be called by objects, such as:

@interface Person:nsobject

(void) Runner;//object method

@end;

int main ()

{

Person *p=[person New];//person is a class that creates an object p.

[P runner];//p Call Runner this method;

return 0;

{

The member variables of our current object can be accessed directly in the object method such as :

@interface Dog:nsobject

{

int _age;//member Variable

}

-(void) dogage;

@end

@implementation Dog

-(void) dogage{

_age=20;//direct access to member variables;

}

@end

Object methods introduced here ...

Class method:

The class method is preceded by "+" such as: + (void) Runner;

Can only be called by a class: for example:

@interface Person:nsobject

+ (void) Runner;//object method

@end;

int main ()

{

[Person Runner];

return 0;

{

Class methods do not need to use member variables, such as 2-digit and:

+ (int) sumN1: (int) n1 andN2: (int) n2;

The class method has the same name as the object method:

You can see that although the method name is the same, one is a class method that is an object method and can also be run.

Dead Loop:

@interface Person:nsobject

{

int age;

}

+ (void) test;

@end

As long as the call in the Mian method is a dead loop

+ (void) test

{

[Person Test];

}

Class methods and object methods can be called to each other:

Class methods and Object methods for iOS development OC

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.