Objective-C study note _ class and Object

Source: Internet
Author: User
Tags word wrap

Objective-C study note _ class and Object

1. Objective-C Overview 2. Object-Oriented Programming OOP Object Oriented Programming 3. Class-defined interfaces in Object OC partially implement partial class and file creation Object so that Object-specific variable operations

1. Objective-C Overview

Cocoa and Objective-C are the core of Apple's Mac OS X operating system. In early 1980, Brad Cox invented Objective-C and intended to combine popular and portable C languages with the Smalltalk language. In 1985, Steve Jobs founded the NeXT company, neXT chose Unix as its operating system and created NeXTSTEP (a powerful user interface toolkit developed using Objectiv-C). In 1988, NeXT began to use Objective-C for development; after Apple acquired NeXT in 1996, NeXTSTEP was renamed to Cocoa and was widely recognized by Macintosh programmers. In 1996, Objective-C became Apple's main programming language.

Objective-C (OC), expanded from C language? Object-oriented programming language. Which is the main programming language for OS X and iOS ?.

Object-Oriented Programming (OOP)
  Process-oriented Object-oriented
Features Analyze the steps to solve the problem, implement the function, and call the function in turn Analyze the objects involved in a problem, define the class according to the function, and make? Complete the program with the object function
Focus Implement Functions Object Design
Language example C Language Objective-C, C ++, Java, etc.

Therefore, object-oriented programming is highly scalable and reusable. This is evident in the future development process.

Three Types and objects

Class and object are the core of object-oriented. Step: define a class, create an object, and use an object.

Class: Abstraction of things with the same features and behaviors.

The object is a class instance.

Class is the object type.

Definition of classes in OC

The definition class consists of two parts: the Interface part (. h file) and the Implementation part (. m file ).

Connected? Part (. h file): Declares the features and behaviors of the class. Implementation part (. m file): internal implementation. Interface Section

Connected? Partial flag: @ interface... @ End

Purpose: declare the instance variables and methods of the class, that is, features and behaviors.

Including: Class Name, parent class name, instance variable, method, etc.

#import

/ * Class declaration:
 * Declaration: instance variables and methods
 * /
@interface Person: NSObject / * Person class name
                              * NSObject parent class name
                              * /
/ * Declare instance variables, in {} * /
{
    @public / * Instance variable visibility modifier * /
    / * Name * /
// char name [20];
    NSString * _name; / * Use NSString for OC string
                      * * The description is a pointer
                      * Instance variables must start with _
                      * /
    NSString * _sex;

    / * Age * /
// int _age;
    NSInteger _age; / * NSInteger is equivalent to the int type in C * /
}

/ * Declaration method * /
/ * Say the way to say hello * /
-(void) sayHi; / * Method declaration is similar to function declaration
                *-Number cannot be omitted, it is called-number method.
                * /

-(void) info;
@end
Implementation part
Partial realization of the logo: @implementation… @end

Action: The implementation method, that is, the behavior of the implementation class.

#import Person.h

@implementation Person
/ * Method implementation
 * The method must be implemented after the declaration, otherwise a warning will appear during compilation
 * /

-(void) sayHi
{
    printf (Hello!
); // C language output
    NSLog (@Hello!); / * @Equivalent to a logo of OC
                       * No need to add later
', Word wrap
                       * /
}

-(void) info
{
    NSLog (@NAME:% @, Gender:% @, Age:% ld, _name, _sex, _age); //% @ is equivalent to referring to
}
@end
Classes and files
Class: @interface… @end @implementation… @end

File: .h is called an interface file or header file, and .m is called an implementation file. The default settings are as follows:

Use the class name to name the file. The interface part of the .h file management class; the implementation part of the .m file management class
The file has nothing to do with the nature of the class.

Create object
The class is a template, the object is a concrete manifestation, and any object takes up memory space.

There are two steps to create an object: allocate memory space and initialize.

Allocate memory space: Allocate memory for objects according to the instance variables declared in the class, set all instance variables to the default value of 0, and return the first address.

Initialization: Set an initial value for an instance variable of an object.

Allocate memory space: Person * p = [Person alloc];

Initialization: p = [p init];

Usually these two operations require continuous writing: Person * p = [[Person alloc] init];

+ (id) alloc; + Indicates that this method belongs to the class and can only be executed by the class. The id return value type represents any type of object, that is, a created object.

-(id) init;-indicates that this method belongs to the object and can only be executed by the object. The id return value type indicates the initialized object.

Make object
The pointer stores the first address of the object and refers to the object. In OC, pointers are used to refer to objects for operations.

Person * p = [Person alloc]; first execute "=" right

[Person alloc] The return value is the object's address, that is, the object. p is a pointer variable of the same type as the object, which stores the first address of the object and refers to the object.
#import
#import Person.h
int main (int argc, const char * argv []) {
    @autoreleasepool {
/ * 1. Define the class
 * 2. Create an object
 * 3. Target users
 * /

/ * Define the class and create a .h and .m file with the class name
 * .h declaration: instance variables and methods
 * .m Implementation part: Implementation method
 * /

/ * Create object
 * 1. Import .h header file
 * 2. Open up memory space
 * 3. Initialization
 * /
        / * Person: class name
         * personOne: object name
         * alloc: method name, function to open up space. + method, only class can use, return value type id, object type.
         * /
        Person * personOne = [Person alloc];
        personOne = [personOne init]; / * initialization, init-method, method used by object, return value type id, object type * /

        / * Allocate memory space and initialize * /
        Person * personTwo = [[Person alloc] init];

        / * Use object (method) * /
        NSLog (@% d,% s, __LINE__, __func__); // number of lines call the module
        [personOne sayHi];
        [personOne info];
 }
    return 0;
}

Four instance variable operations
Instance variables only need to be set a little during initialization, and they need to be set later.

Instance variables distinguish visibility, there are three types of public, protected, and private, and the default visibility is protected.

@public: Instance variable access modification symbol (public); public modified instance variables can be accessed directly using "—>"


#import
#import Person.h
int main (int argc, const char * argv []) {
    @autoreleasepool {
        / * Assignment operation * /
        Person * personOne = [[Person alloc] init];
        [personOne info];

        / * Name assignment * /
        personOne-> _ name = @Zhang San;
        personOne-> _ sex = @man;
        personOne-> _ age = 20;

        / * Value operation * /

        / * Output name * /
        NSLog (@NAME:% @, personOne-> _ name);
        NSLog (@ 性 性:% @, personOne-> _ sex);
        NSLog (@age:% ld, personOne-> _ age);

        [personOne info];
    }
    return 0;
}

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.