OBJECT-C Classes and objects

Source: Internet
Author: User

Object-oriented features: encapsulation, inheritance, polymorphism

The access control character, @private @package @protected @public

Classes and objects, classes are abstractions of a batch of objects.

1. Steps to define a class

Interface section: Define the member variables and methods that the class contains

Implementation section: Providing implementations for this class of methods

@interface Myclass:nsobject

{

int _count;

int _data;

nsstring* _name;

}

-(ID) initwithstring: (nsstring*) AName;

+ (MyClass) createmyclasswithstring: (nsstring*) AName;

@end

2. Method signature keyword

-(void) Ins: (ID) anobject atindex: (nsuinteger) index;

void: return value type

Id+nsuinteger: Formal parameter type

Ins+atindex: Method signature keyword

Anobject+index: Form Parameters according to

Note: The Object-c method signature Colon has special meanings, such as the Foo method and Foo: The method is different, the former does not have a colon to represent a method without a formal parameter declaration, the latter with a colon, which means that he is a method with a formal parameter declaration, and Foo:bar: There is another way to represent something, Indicates that it is a method with two parameters, where the label of the second parameter is bar. 、

3. Instantiation of a class

@implementation myclass{

int _count;

int _data;

nsstring* _name;

}

-(ID) initwithstring: (nsstring*) aname{

Method body

}

+ (MyClass) createmyclasswithstring: (nsstring*) aname{

Method body

}

@end

The class name of the class implementation section must be the same as the class name of the class interface section to indicate that this is the interface part and implementation part of the same class.

The class implementation section can also use ": Parent class" After the class name to indicate that a parent class has been inherited, but it is generally not necessary

The class implementation section can also declare its own member variables, but these member variables are accessible only within the current class, so declaring member variables in the class implementation section is equivalent to defining a hidden member variable.

The class implementation section must provide a method definition for each method of the class declaration section, which consists of the method signature and the method body, and the implementation section can provide additional method definitions in addition to the methods of implementing the class interface part definition, which are not defined in the interface section, but are only implemented partially in the Class implementation section. There is a strict order of execution between multiple executable statements in the method body.

#import "FKperson.h"

@implementation Fkperson

{

Define a member variable that can only be used in the implementation section

int _testarray;

}

-(void) SetName: (nsstring*) n andage: (int) a{

_name=n;

_age=a;

}

-(void) say: (nsstring*) content{

NSLog (@ "%@", content)

}

-(NSString) info{

}

-(void) test{

}

+ (void) foo{

}

Object

1. The object is generated from the use

To define a variable to create an Object call class method

fkperson* person;

Person=[[fkperson alloc] init];

Person say:@ "Hello World";

Person setname:@ "Jack" ANDAGE:21;

nsstring* Info=[person Info];

[Fkperson foo];

The + function is a class method, the caller is a class;-the function is an object method, and the caller is an object.


OBJECT-C 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.