"Objective-c" 02. First OC Language class

Source: Internet
Author: User

02. First OC Language class

In the Java language, creating a class requires only one file, whereas in the OC language, creating a class requires two files. m files and. h files.

The. h file is a declaration class used to declare member variables and methods. Use keyword @interface to decorate, end with @end.

The. m file is an implementation class used to implement member variables and methods, decorated with keyword @implementation to @end end.

Methods: In OC language methods need to use + or-to indicate the type of the method.

+ Represents a static method.

-Represents a dynamic method.

All methods in the. h file are public and cannot be changed.

Member variables: member variables can be divided by scope: public protected private

All classes will eventually inherit the NSObject.

Student.h file

#Import <foundation/foundation.h>//The Student class inherits the NSObject class, so you need to refer to the Foundation.h file under the foundation framework.

@interface Student:nsobject {//declares the student class and then inherits from the NSObject class. Place the member variable in {}

Int age;//The declaration of the member variable is usually placed in the header file,

Int height;

@public//Declaring member variables of different types

Int weight;

}

@end

STUDENT.M file

#import "Student.h"//must refer to the header file of the class, otherwise cannot implement the Declaration, assuming that the header file is the name, the project file is the body, no name, no body.

@implementation student{//Implement student class

}

@end

Add method

To preserve object-oriented encapsulation, in the Java language, both the Get method and the Set method use GetName and SetName. In OC, the Get method requires a method with the same name as the original, such as the age method to get the age variable using the age function.

#import <foundation/foundation.h>

@interface student:nsobject{

Int age;

Int height;

}//{} start terminating with member variable

-(int) age; The Get method for//age, which is represented by the return value type (int)

-(void) Setage: (int) newage; No return value, parameter: Draw (int) is the parameter data type, formal parameter name is newage. The return value or the type of the parameter should be wrapped with ().

-(void) Setage: (int) newage andheight (int) newheight; In OC One: corresponds to a parameter, and: is also part of the method name, so the method name is Setage:

-(void) Setage: (int) newage andheight: (int) height; Add a method to manipulate both age and height

Method name is setage:andheight: Dynamic method, no return value, there are two parameters, Andheight can be omitted. The method name can be (void) Setage: (int) NewAge: (int) height;

The method of implementation in STUDENT.M

#import "Student.h"

@implementation Student

-(int) age{

Return age;

}

-(void) Setage: (int) newage{

Age=newage;

}

@end

Student.h and STUDENT.M are all there, this Student class is complete, and the next step is to use the Student class to create an instance.

@Import <foundation/foundation.h>

@import student.h

int main (int argc, const char * argv[]) {//In the OC language, all object storage types are pointers, that is, open up a piece of memory space, then deposit the appropriate type data, and then point to it with the pointer.

@autoreleasepool {

Student *stu=[[student Alloc]init]; In the OC language, the method can be called by invoking the principal method name of the method. To create an object in OC, you first need to open up a memory space with the class that created the object, and then initialize the class so that the object is available.

[Stu release];//Object Destruction

}

Return 0;

}

Student Stu=[student alloc]; A memory space is opened with the student type and a Stu pointer is created to point to the memory space. The memory space you just created is not available for initialization and must be initialized before it can be used.

Stu=[stu init];//Initializes the memory space pointed to by the Stu pointer and returns the initialized Stu address to the pointer. A static variable can be called by a class. Dynamic variables can be invoked with objects.

Therefore, calling methods in the OC language actually requires "calling the method principal method name"

Destroys an object that needs to be destroyed manually after the method call ends.

The previous Stu called some other methods, such as Alloc and INIT, not because the Foundation.h file was called, but because the student parent class is nsobject, so these methods can be called.

These methods and member variables are called when the Stu object of the student class is created.

1 Import <Foundation/Foundation.h>

2 #import "Student.h"

3

4 int main (int argc, const char * argv[])

5 {

6 @autoreleasepool {

7 Student *stu = [[Student alloc] init];

stu->age=18; Only member variables that are public can be accessed directly in the form of pointers.

[Stu setage:18];//protected member variables can be called by the set method.

Int newage=[stu->age];

Int Newage=[stu Age];

"Objective-c" 02. First OC Language class

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.