Learn objective-C (3) together -- How to declare and define classes

Source: Internet
Author: User


Through the hello World Program on the front, we know how to include the header file and the basic usage of nslog. Starting from this section, we will explain how to declare and define classes through a simple class, and the length of member functions in the class.

Add the following three files in home described in Section 1: REC. H, Rec. C, and Main. M:

/*** @file Rec.h* @brief * @author Don Hao* @date 2011-8-21 12:14:13* @version * <pre><b>copyright: </b></pre>* <pre><b>email: </b>hao.limin@gmail.com</pre>* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>* <pre><b>All rights reserved.</b></pre>* <pre><b>modification:</b></pre>* <pre>Write modifications here.</pre>*/#import <Foundation/Foundation.h>@interface Rec : NSObject {    int length;    int width;}-(void) setLength: (int) len setWidth: (int) wid;-(int) getLength;-(int) getWidth;      +(void) testStaticMethod:(NSString*) nsstr printCStr:(const char*) cstr;@end


/*** @file Rec.m* @brief * @author Don Hao* @date 2011-8-21 12:14:13* @version * <pre><b>copyright: </b></pre>* <pre><b>email: </b>hao.limin@gmail.com</pre>* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>* <pre><b>All rights reserved.</b></pre>* <pre><b>modification:</b></pre>* <pre>Write modifications here.</pre>*/#import "Rec.h"@implementation Rec-(void) setLength: (int) len setWidth: (int) wid{    NSLog(@"SET: Length=%d, Width=%d\n", len, wid);length = len;width = wid;}-(int) getLength{    return length;}-(int) getWidth{    return width;} +(void) testStaticMethod:(NSString*) nsstr printCStr:(const char*) cstr{    NSLog(@"This is NSString: %@  This is C String: %s\n", nsstr, cstr);}@end
/*** @file main.m* @brief * @author Don Hao* @date 2011-8-21 12:14:13* @version * <pre><b>copyright: </b></pre>* <pre><b>email: </b>hao.limin@gmail.com</pre>* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>* <pre><b>All rights reserved.</b></pre>* <pre><b>modification:</b></pre>* <pre>Write modifications here.</pre>*/#import "Rec.h"int testCFun(int length, int width){    printf("C FUN: %d %d\n", length, width);}int main(int arvc, char* argv[]){    testCFun(1, 2);        Rec* r = [Rec new];        [r setLength:10 setWidth:20];        NSLog(@"Length:%d\n", [r getLength]);    NSLog(@"Width:%d\n", [r getWidth]);        NSString* nsstr = @"NSSTRING";    const char* cstr = "CSTRING";        [Rec testStaticMethod:nsstr printCStr:cstr];    return 0;}

Makefile content (if you are not clear about it, refer to section 1 ):

gen:gcc -o main main.m Rec.m -I/GNUstep/System/Library/Headers/ -fconstant-string-class=NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base

The running result is as follows:

The following sections describe the relevant content one by one.
If you are used to the C/C ++/C #/Java code style, you may find it unacceptable to see the objective-C code. There is no need to feel like rejection. It's good to get used to it later.

This section describes how objective-C declares and defines classes.

There is a rec class (rectangle) above, and we put the class declaration. h header file (class definitions in C ++ can be placed in. h), the class definition is put in. M file.
1. Class Declaration

Class declaration format:

@ Interface Class Name: parent class {// attribute} // method declaration @ end

The class must be directly or indirectly inherited from nsobject, and @ interface is used to indicate that this is the definition of the class, and the colon behind the class name is inherited (same as C ++ ), put the attribute in the Large arc. After the Large arc, It is the class method, and finally @ end (this can be left empty, but in order to let others know that the class definition ends here, write better ).

2. Class Definition

Class Definition Format:

@ Implementation class name // method definition @ end

3. Example

The preceding example defines the class REC:

A. Class rec inherited from nsobject

B. There are two attributes: length and width.

C. There are four methods: the specific method is explained in the next section.

D. Modify the method through +/-. If it is-, it indicates that the method belongs to the object. If it is +, it indicates that the method belongs to the class (that is, the static method)


The difference between objective-C and C ++ classes is:

A. c ++ uses class to identify the class, while objective-C uses @ interface/@ implementation to identify the class;

B. C ++ uses static to identify whether it is a static method, while objective-C uses ++/-to distinguish it;

C. c ++ class methods are defined in the Large arc of the class, while objective-C is outside the Large arc;

E. c ++ has access restriction attributes such as public, private, and protected on attributes and methods. In objective-C, the attribute is protected and the method is public;

F. C ++ supports multi-inheritance, while objective-C does not support multi-inheritance;


Learn objective-C (1) -- install the development environment and hello World in Windows

Learn objective-C (2) together -- Hello World

Learn objective-C (3) together -- How to declare and define classes

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.