Objective-C learning 01-class declaration and implementation, objective-c01

Source: Internet
Author: User
Tags print object

Objective-C learning 01-class declaration and implementation, objective-c01

Objective-C is a programming language invented by Brad Cox in early 1980. It is the same as C ++ in its generation, all of them are extended by adding object-oriented features on the basis of C. Objective-C supports Object Oriented Programming. Its main features include encapsulation, inheritance, and polymorphism.

Since it is an object-oriented programming language, it also has classes/objects, class methods/object methods, member variables and other concepts.

1. Syntax Introduction

1.Class

Class is the abstraction of things with the same features and behaviors (functions). It is equivalent to a data Type, but this Type is called Class Type ), objects are variables created using these types. objects are class instantiation.

Creating a class is equivalent to defining a data type. The variables created using this data type are called objects.

In OC, two files are generally used to describe a class:

1>. h: The Declaration file of the class, which is used to declare member variables and methods. The class Declaration uses the keywords @ interface and @ end.

Note: The method in. h is just a declaration and does not implement the method. That is to say, it only describes the method name, the return value type of the method, and the parameter type received by the method, and does not compile the code inside the method.

2>. m: class implementation file, used to implement the method declared in. h. Class implementation using the keyword @ implementation and @ end.

2.Method

The method is equivalent to a function in C language, but OC is no longer called a function.

1> methods and implementations must start with + or-

  • + Indicates that the class method (static method) is a class method modified by the plus sign "+". This method can only be called by this class (that is, called by class name)
  • -Indicates that the object method (dynamic method) modified by minus "-" belongs to the object method. This method can only be called by the object created by the current class.

3.Member variables

There are three common scopes of member variables:

1> @ public global access

2> @ protected can only be accessed inside the class and in the subclass.

3> @ private can only be accessed within the class

2. Analysis of related classes

1. Person. h-class declaration File

# Import <Foundation/Foundation. h>
@ Interface Person: NSObject {// instance variable @ public NSString * _ name; // name NSInteger _ age; // age NSString * _ gender; // gender}
// Three methods are defined here: speaking, eating, and sleeping.-(void) sayHello;-(void) eat;-(void) sleep; @ end

A. @ interface means that Person is the type name, colon: indicates the inherited class, And NSObject is the base class of all classes in OC. The system will automatically generate these classes after they are created, you do not need to manually write the code.

B. Write the features of this class in braces {} (that is, the instance variable: the attributes of the class instance (object). The instance variables must be written into the braces.

C. @ public the instance variable can be accessed outside the class after being modified by @ public.Class externalFiles other than the. h and. m files are called the class's external, and the class's internal is. h and. m.

D. NSString * _ name; // NSString is a string class in OC and belongs to a non-basic data type. When using it to define variables, add *

NSInteger _ age; // NSInteger is an integer in OC and is a basic data type. Therefore, when defining instance variables *

NSString * _ gender; // gender

For the data type in OC, see blog: http://www.cnblogs.com/Harbingwang/p/4995920.html

Note: // The type in OC (that is, the non-basic data type). When declaring a variable, you must add the '*' sign.

// The basic data type of C language does not require a star number *

// All instance variable names start with '_' (note)

2. Implementation file of the Person. h-class

# Import "Person. h" // implementation. Here we write the implementation part of the class @ implementation Person
// Voice-(void) sayHello {// print the name, age, and gender of the current object NSLog (@ "Hello! My name is % @. This year, my name is % ld. The gender is % @ ", _ name, _ age, _ gender);} // (void) eat {NSLog (@ "eat spicy food at night ");}
// Sleep-(void) sleep {NSLog (@ "it's time to go to bed");} @ end

Note:

-(Id) init {_ name = @ "Black Widow"; _ age = 18; _ gender = @ "female ";
Return self; // who calls this method, self points to who}

 

5. NSLog is the output Statement of OC. When using NSLog, note that the output content must be written in the middle of @ "", @ cannot be omitted; OC print string: In OC, A string is not a string, but a String object. It belongs to the object created by NSString.

% @ Is a placeholder for any type of object. As long as it is a print object, % @ is used as a placeholder.

6. @ interface and @ end are used together. @ Implementation and @ end are also used together.

 

3. Create an object

Create an object in main. Before creating the object, we need to introduce the header file of the Person class into main and introduce the header file # import "Person. h"

1. Create an object using a class. 1>. allocate memory space to the object. 2>. initialize the object.

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

1>. [Person alloc] opens the memory space in the heap area and returns the first address of the memory space.

The alloc method called above will return the Person object p allocated with memory. On the left side of the equal sign, a pointer Variable p pointing to the Person type is used to receive this object. Pay attention to the * on the left side of p. All OC objects are received using pointer variables. Remember: when defining a variable using class name, the class name must be followed by a * sign.

2>. Call the construction method init of the person object for initialization.

2. assign values to instance variables of an object

P-> _ name = @ "HarbingWang"; // assign a value to the name

P-> _ age = 23; // assign a value to the age

P-> _ gender = @ "male"; // assign a gender Value

 

Use object call Method: [] Braces are the method call characters in OC. The method caller is written on the left side of the braces, the method name is written on the right side of the brackets, and spaces are left in the middle.

[P sayHello]; // call the speaking Method

OC uses the message sending and receiving mechanism. The statement [p sayHello] is translated to send a sayHello message to the object p, and then p checks whether it can execute the message, if the sayHello method can be called, an error (Crash) will be reported if the method cannot be called)

 

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.