OBJECTIVE-C (Declaration and implementation of three methods)

Source: Internet
Author: User

The Declaration and implementation of OC method

The declaration of the OC method is @interface outside the curly braces @end above

The implementation of OC method @end above in @implementation

In the OC method, one parameter corresponds to a colon

Method Name:

Example fangfaming: (Colon is also part of the method name)

-Return value type method name: Parameter name: (parameter type) argument name: (parameter type) parameter;

Example-(int) FANGFAMING:SUMWITHNUM1: (int) NUM1 andNum2: (int) num2;

?? Space here

Easy to make mistakes

1. Write only the declaration, do not write the implementation

2. @end this end tag is missing.

3. The declaration or implementation of a class cannot be written in the function of C

4. Declaration of attributes must be written in curly braces

5. You cannot assign a value directly when declaring a property

6. Declaration and implementation cannot be nested

7. The Declaration of the method can only be written under curly braces, @end above

8. '-[person Run]: Unrecognized selector sent to instance 0x100109c80 ' is that the object did not find run this method

9. When creating a class, be sure to write: NSObject so that the class has the ability to create objects.

Summarize

@import <Foundation/Foundation.h>

Declaration of the class

@interface iphone:nsobject//enables classes to have the ability to create objects

{

@public

int _model;//Model

}

Methods are divided into two types: Object methods and Class methods

Notice Object Methods

1. Object methods with-start, 2. All data types are placed in (), and only data types are placed in () 3. Method names follow the hump principle 4. If there is no parameter, there is no need to write 5 for the method name. If it is a method with parameters, one parameter corresponds to one: A colon is part of a method name 6. In general OC, the method name is written like a sentence, to be known.

No parameter no return value

-(void) lookmyphone;

No parameter has a return value

-(char *) receiptmessage;

No return value with parameter

-(void) SendMessage: (char *) message tophonenumber: (char *) number;

Have a parameter return value

@end

Implementation of the class

@implementation Iphone

-(void) lookmyphone{

NSLog (@ "model is%i", _model);

}

-(char *) receiptmessage{

return "Hello";

}

-(void) SendMessage: (char *) message tophonenumber: (char *) number{

NSLog (@ "You are giving%s, send%s", number,message);

}

@end

int main (int argc, const char * argv[])

{

An object in OC can be a member variable of another object. By who who belongs to judge, if put into context, in line with people's habits, the front is the following member variables.

1. Open up Storage 2. Initialize member variables (integers are initialized to zero, and objects in OC are initialized to null (nil) 3 when they are member variables. Return the address of the space

iphone *iphone1 = [iphone new];

Assigning a value to a member variable

Iphone1->_model = 2;

Build a second object

iphone *iphone2 = [iphone new];

Iphone2->_model = 5;

Summary: 1. A class can have multiple objects, and the objects do not affect each other

2. A member variable or an object method of an object can only be called by a pointer.

3. A member variable or method of an object can only be called by this class or object.

return 0;

}

OBJECTIVE-C (Declaration and implementation of three methods)

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.