Object-c Object-Oriented implementation class

Source: Internet
Author: User

Generally involves the object-oriented C#,java are inevitably involved in classes, C # The suffix name for the class in the. Cs,java is. Java,object-c typically uses two files to describe a class with a suffix of. h as the class's declaration file, which declares member variables and methods,. m files are primarily used to implement methods in declarations, and Java and C # declaration classes use the Class keyword, OC Declaration class with the word @interface and @end, the implementation of the class is @implementation and @end, with the other a bit different, method definition, method calls are not the same, a little bit of a look at it:

New Cocoa Class:

Language selection OC, Class name book:

Code in Book.h:

#import <Foundation/Foundation.h> @interface book:nsobject{    nsstring *_author;    NSString *_publishdate;} @property int number;-(void) getdetial; @end

Code in BOOK.M:

#import "Book.h" @implementation book@synthesize number;-(void) getdetial{    NSLog (@ "on Programmer's Professionalism");} @end

The invocation in the main function:

        Book *book=[book new];    [Book getdetial];    Book. number=100;    NSLog (@ "Number of books:%d", book.) number);

The code here temporarily does not parse, look at another piece of code:

typedef enum {Man    ,    woman} Sex, @interface person:nsobject{    @public    nsstring *name;    NSString *school;    Sex sex;    int age;} -(void) say;-(int) sum: (int) NumA secondnumber: (int) NumB; @end @implementation person-(void) say{NSLog    (@ "say more and more wrong, Less say less Wrong ");} -(int) sum: (int) NumA secondnumber: (int) numb{    return numa+numb;} @end

The call in the main function, the code is as follows:

Person *person=[person new];    person->age=25;    person->sex=man;        NSLog (@ "Age:%d, Gender:%@", person->age,@ "test");    [person say];    int B=[person sum:10 secondnumber:10];    NSLog (@ "Two numbers added and is:%d", b);

The member variables in the class are all in {}, and if you want to invoke them when initializing the class, access them directly using the-----if the scope of the variable is accessible, generally the scope is @public, @protected, @private, if you have contact with other languages, Public is accessible, and protected is accessible to both the parent and child classes, and private to the class itself.

C # protects fields by manipulating properties, and in OC, so the first piece of code appears @property and @synthesize;

The method definition + means that the class method is also a static method-the object method:

-(int) sum: (int) NumA secondnumber: (int) NumB

int is the return value type, followed by a function that is: (type) name, if it is more than the space after the repetition, as above, very good understanding; new languages are used in many, but generally the form of a new object is:

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

The line pen hurriedly, unavoidably omission, if have improper, many advice, first thanked ~

Object-c Object-Oriented implementation class

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.