The core grammar of the object-----OC facing the end of the love paper

Source: Internet
Author: User

A little guilty, the months of neglect, but also wasted a lot of time.

Next I read some notes about the core grammar, probably not all may be wrong, I hope you can point out.

One, dot syntax

The essence of point syntax is invocation.

For example:

Person *p = [person new];

Assign value

P.age = 10; equivalent to [P setage:10]; Call setter

Take value

int a = p.age;//is equivalent to [P age]; Call getter

Ii. Scope of member variables

Direct access to @public anywhere

@private can only be accessed directly in the object method of the current class

@protect object methods for the current class and subclass. Although protected, it has a method in the parent class in the memory sub-class

Iii. @property and @synthesiz

1. Automatically generate _age.

2. Automatic generation of _age setter and getter declarations

@property int age;//write on type and name

/* Equivalent to

-(void) Setage: (int) age;

-(int) age;*/

Represents a String

@property Nsstring*name;

Implementation of generating setter and getter methods

@synthesize age = _age;

can also be attached to write

@synthesize Age =_age,name = _name;

Note:

Generally omit @synthesize, write only @property

and the Declaration and implementation of the setter and getter are automatically generated.

Four, universal pointer ID

ID is a pointer to the general form of

id = nsobject*

ID d = [person new]

Five, the construction method

belongs to the object method, and the minus sign begins.

New complete creation of an available object

① Allocating storage space ② initialization

You are no longer using new to create objects. Change to the following method

1. Call +alloc to allocate storage space

2. Call-init to initialize

Person *p = [[Person alloc] init];

Vi. overriding construction methods

Purpose: In order for the object to be created, the member variable will have some fixed values

Overriding the-init method * * *

-(ID) init//id is designed so that any object can be called NSObject

{

Be sure to call the Init method back to Super to initialize the member variables and other properties declared in the parent class

if (self = [super init]

The following initialization is possible if the object is initialized successfully

{_age = 10;//all member variable initialization values are 10

}

Return self;//returns an object that has already been initialized

}

Vii. Custom Construction methods

Custom Construction methods:

1. Must be the object method, must start with a minus sign

2. The return value must be the ID type

3. Method names begin with Initwith

For example

@interface Person:nsobject

@property NSString *name;

@property int *age;

-(ID) Initwithname: (nsstring*) name andage: (int) age;

@implementation person

-(ID) Initwithname: (nsstring*) name andage: (int) age;

{if (self=[super init])

{_name=name;

_age=age;

}

return self;

}

int main ()

{Person *p = [[Person alloc] initwithname:@ "Rose" andage:30]

return 0;

}

The nature of the class

Use the person class to create objects of type person

Person *p = [[Person alloc] init];

Gets the in-memory class object, which is the person class object

Class C = [P class];

Can also be written as class C = [Person class];

Note:

The class itself is also an object, a class-type object, or a class object,

Create a person class object with class

Create an object of person type with person

The core grammar of the object-----OC facing the end of the love paper

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.