Objective-c class, instance member, static variable, object method, class method (static method), object,

Source: Internet
Author: User
Objective-c class, instance member, static variable, object method, class method (static method), object,




First, the class

In iOS, the declaration of a class is separated from its implementation, that is, it cannot be written in the same file, the declaration is placed in the. h file, and the implementation is placed in the. m file. Introduce. h files in the implementation file, #import "Xxx.h"
Declare a class:
#import <Foundation/Foundation.h>
@interface Person:nsobject

@end

To implement a class:

#import "Person.h"
@implementation person

@end

Second, instance members

In the iOS class the bar variable is called the instance variable, and the default permission is protected, in the class can only declare the instance variable, must be able to declare the method. And you cannot declare static instance variables in. h files, which can only be declared and used in. M.
Eg:
#import <Foundation/Foundation.h>

@interface person:nsobject{
int age;
nsstring* name; A string in iOS
static int dwint; Error, can ' t
}
@end

Third, static member variables
Static instance variables cannot be declared in the. h file, and can only be declared and used in. M.
Eg:
#import "Person.h"
@implementation person

static int dwint=20;

@end

Iv. Object Methods
Object methods cannot be declared in parentheses, can only be declared outside of parentheses, and precede with-.
#import <Foundation/Foundation.h>

@interface person:nsobject{
int age;
nsstring* name; A string in iOS
}
-(int) getage;
-(nsstring*) getName;
-(void) Setage: (int) _age;
-(void) SetName: (nsstring*) _name;
-(void) Setage: (int) _age andname: (nsstring*) _name;
@end


Implement. M
#import "Person.h"

@implementation person
static int dwint=20;
-(int) getage{
return age;
}
-(nsstring*) getname{
return name;
}
-(void) Setage: (int) _age{
Age=_age;
}
-(void) SetName: (nsstring*) _name{
Name=_name;
}
-(void) Setage: (int) _age andname: (nsstring*) _name{
Age=_age;
Name=_name;
}
+ (int) getstatic{
return dwint;
}
@end
Five, class method
Class methods cannot be declared in parentheses, can only be declared outside of parentheses, and precede with +.
#import <Foundation/Foundation.h>

@interface person:nsobject{
int age;
nsstring* name; A string in iOS
}
-(int) getage;
-(nsstring*) getName;
-(void) Setage: (int) _age;
-(void) SetName: (nsstring*) _name;
-(void) Setage: (int) _age andname: (nsstring*) _name;
+ (int) getstatic;
@end

Implement. M

#import "Person.h"
@implementation person
static int dwint=20;
-(int) getage{
return age;
}
-(nsstring*) getname{
return name;
}
-(void) Setage: (int) _age{
Age=_age;
}
-(void) SetName: (nsstring*) _name{
Name=_name;
}
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.