"Dark Horse Programmer" class and Object (OBJECTIVE-C)

Source: Internet
Author: User

I. Concepts of classes and objects

Two of the key concepts in OC are classes and objects, and the relationship between classes and objects is like the relationship between a mold and an item made with this mold. A class gives a uniform definition of all its objects, and each of his objects is an entity that conforms to that definition, so the relationship between classes and objects is abstract and concrete.

To create an object:
An existing class is required to create an object, and the object needs to articulate the attributes and functionality (behavior) format that it should have [behavior performer behavior name]

To create a class:

There are three conditions to be concerned:

Thing name (class name): Person (PErson)

Properties: Height (height), age, etc.

Behavior (function): Run (run), eat (EAT)

Note: 1. General nouns are classes

2. Objects with the same (or similar) attributes and behaviors can be abstracted out of a class

3. The first letter of a class name must be capitalized ;

4. Can not be underlined, if there are multiple English words, with the hump marked

5. Which class is the most clear, the behavior is designed into that object

Ii. Declaration and realization of class
    • Complete write a class/function, requires two steps, class declaration and implementation (definition)
    • Declaration of the class:

@interface class Name

Cases:

@interface Car

{

  This declares the properties of the object

}

This declares some behavior (methods)

@end

    • Implementation of the class:

@implementation class Name

//Here are some ways to implement

@end

Cases:

@implementation Car

Implementation of the method

@end

Code exercises (creation and implementation of classes):

//OC in the NSObject statement in <Foundation/Foundation.h>
#import <Foundation/Foundation.h>//Class Name: car//property: Number of tires wheels, speed speed//method: Run//define a function: need to declare and implement a function//define a class: the Declaration and implementation of a class is required//the declaration of the class//OC keyword from the beginning of the @,//The first letter of the class name Must start with a capital letter, if there are multiple words at the beginning of each word must be capitalized, to use the hump mark//NSObject to make this class have the ability to create objects
//@interface used to declare member variables/instance variables, and object methods. @interface car:nsobject{//@public allows the external pointer to indirectly access the member variable @public int wheels; int speed;}/ * 1 OC The object method must begin with a minus sign. 2 The return value of the method to be thought must be enclosed in parentheses. 3 method's claim rule: (return value) method name: Parameter 4 OC method brackets Enclose data type only * /-(void) run; @end//class implementation:@implementation Car//Method implementation: Clear the method in the declaration-(void) run{NSLog (@ "The car ran up"); @endint Main (int argc, const char * argv[]) {only classes can be used to create objects in OC//in OC you want to write a [behavior performer's behavior name]///OC object can only be manipulated by a pointer//[Car new] Creates a new object each time, and returns the new object itself (object 's address)Car *p = [car new];//through pointers to access member variables//for P-pointing class of the Wheels property assignment value 4,//to the P-pointing class of the speed attribute is assigned to a value ofP--wheels = 4; P-speed = 300;//Send a run message to the class that P refers to to invoke the implementation of the Run method. [P run]; To access a member variable inside a class, you must use a pointer to access the NSLOG (@ "number of tires =%d, speed =%d", p->wheels,p->speed); return 0;}

Member variables and attributes must be written in the middle of the @interface and @end.

● member variables are not allowed to initialize.

member variables are not allowed to use static adornments.

the implementation of the class can be written behind the Mian function, but the declaration must be written in front of the main function for declaration.

"Dark Horse Programmer" class and Object (OBJECTIVE-C)

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.