"Introduction to OC-Classes and objects"

Source: Internet
Author: User

OC is a kind of object-oriented thought compared with C language. OC fully compatible with C language

Most of the classes in OC are preceded by the prefix NS, which comes from the Jobs NeXTSTEP team, which retains all the basic syntax when acquired by Apple and merges into the foundation

OC language with. m for file suffix

Absolute majority class in OC inherits from NSObject OC is single inheritance (can use protocol to achieve multi-inheritance effect)

One, the keyword @ formatted as an object

@ "Hello, world!" As a String object

@[@ "123", @ "234"]; Array

@selector () Selector

@property Properties

ID denotes any type

Second, class

declares that the person class inherits from NSObject

@interface Person:nsobject

{//member variable (shadow with struct), global variable (it is a global variable)

Nsinteger age;

NSString *name;

}

Realize

@implementation person

@end

In many cases we will rewrite the Init method, for example:

-(Instancetype) init

{

self = [super init];

if (self) {

Do something, set a default value preset condition

Age = 20;//is automatically set to 0 if it does not exist

Name = @ "Zhangsan"; The method of assigning constant value

/*

Age = 0;

name = nil;

Char *s = NULL

*/

name = [[NSString alloc]initwithformat:@ "Zhangsan"];//is typically assigned in this way

NSLog (@ "%ld", age);

NSLog (@ "%@", name);

}

return self;

}

Third, object initialization

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

Person *per = [[The person new];//is the same as the above effect

The memory allocated object OC is automatically allocated memory and then initializes the allocated memory to the desired object Alloc allocate memory init initialization

Note: Person *per1 = per; It's just a change of name or assignment.

Four, object output

objects are output with%@

NSLog (@ "%@", per);

V. Methods

Format:

Caller return value method name (with parameter with colon) parameter type formal parameter name

-(void) run: (int) distance;

Call:

[Object Method Name]

[Per run];

"Introduction to OC-Classes and objects"

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.