Objective-c Learning Notes _ Classes and objects

Source: Internet
Author: User





    • A objective-c overview
    • Two object-oriented programming OOP object oriented programming
    • Three classes and objects
      • Definition of class in OC
        • Interface section
        • Implementation part
        • Classes and files
        • Creating objects
        • Make an Object
    • Four-instance variable operation




A objective-c overview

Cocoa and objective-c are at the heart of Apple's Mac OS X operating system. At the beginning of 1980, Brad Cox invented Objective-c, a combination of popular, portable C and Smalltalk languages; in 1985, Steve jobs set up next, and next chose Unix as its operating system. Created NeXTSTEP (a powerful User interface toolkit developed using OBJECTIV-C); In 1988, Next started using Objective-c, and after Apple acquired next in 1996, NeXTSTEP was renamed Cocoa, and has been widely recognized by the Macintosh programmer, that 1996 OBJECTIVE-C became Apple's main programming language.

OBJECTIVE-C abbreviation OC, expanded from the C language of the object-oriented programming language. is the main programming language for OS X and iOS OS?.

Two object-oriented programming oop (Object Oriented programming)
Process oriented Object Oriented
Characteristics Analyze the steps to solve the problem, implement the function, call the function in turn Analyze a problem need to participate in the object, according to the function of defining the class, so that the function of the object to complete the program
Emphasis Implementation features Design of the object
Language examples C language Objective-c, C + +, Java, etc.


Therefore, object-oriented programming has good scalability and reusability. This is evident in the future development process.


Three classes and objects


Classes and objects are the object-oriented core. Step: Define the class, create the object, and use the object.



Class: An abstraction of something with the same characteristics and behavior.



An object is an instance of a class.



The class is the type of the object.


Definition of class in OC


The definition class contains two parts: an interface section (. h file) and an implementation section (. m file).


    1. Next section (. h file): Declares the characteristics and behavior of the class.
    2. Implementation section (. m file): A concrete implementation of the internal.
Interface section


Part mark: @interface ... @end



Function: Declares the instance variables and methods of the class, that is, the characteristics and behavior.



Contains content: Class name, parent class name, instance variable, method, and so on.


#import <Foundation/Foundation.h>

/* 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; /* OC string using NSString
                       * * 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 */
/* Declare the way to say hello */
- (void)sayHi; /* method declaration is similar to function declaration
                 * - The number cannot be omitted, called the - number method.
                 */

- (void)info;
@end 
Implementation part


Implementation part flag: @implementation ... @end



The implementation of the law, that is, the realization of the class?


#import "Person.h"

@implementation Person
/* method implementation
  * Must be implemented after the method declaration, otherwise there will be a warning when compiling
  */

- (void)sayHi
{
     Printf("Hello!\n"); // C language output
     NSLog(@"Hello!"); /* @ is equivalent to a logo of OC
                        * No need to add ‘\n‘, wrap
                        */
}

- (void)info
{
     NSLog(@"name: %@, gender: %@, age: %ld", _name, _sex, _age); // %@ is equivalent to the reference object
}
@end 
Classes and files


Class: @interface ... @end @implementation ... @end



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


    1. Use the class name to name the file.
    2. The interface section of the. h file management class; The implementation part of the. m File Management class


There is no relationship between the file and the class nature.


Creating objects


A class is a template, and an object is a concrete representation of any object that consumes memory space.



Create objects in two steps: Allocate memory space and initialize.



Allocate memory space: allocates memory for an object based on an instance variable declared in the class, resets all instance variables to the default value of 0, and returns the first address.



Initialize: Sets the initial value for an instance variable of an object.



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



Initializationp = [p init];



Typically these two operations require ligatures:Person * p = [[Person alloc] init];



+ (id)alloc;+ indicates that this method belongs to a class and can only be class executed. The ID returns a value type that represents an object of any type, that is, a created object.



- (id)init;-Indicates that this method belongs to an object and can only be executed by the object. The ID returns a value type that represents the object that the initialization completed.


Make an Object


The pointer stores the object's first address, which refers to the object. Use the pointer to refer to an object in OC to manipulate it.



Person *p = [Person alloc];Execute "=" to the right first


    1. [Person Alloc] The return value is the object's? Address, which is the object.
    2. P is a pointer variable of the same type as the object, storing the object's first address and substituting the object.
#import <Foundation/Foundation.h>
#import "Person.h"
Int main(int argc, const char * argv[]) {
    @autoreleasepool {
/* 1. Define the class
 * 2. Create an object
 * 3. Use objects
 */

/* Define the class, create a .h and .m file for the class name
 * .h statement: instance variables and methods
 * .m implementation part: implementation method
 */

/* Create object
 * 1. Import .h header files
 * 2. Open up memory space
 * 3. Initialization
 */
        /* Person : class name
         * personOne : object name
         * alloc : method name, role to open 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 the 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 rows calling module
        [personOne sayHi];
        [personOne info];
 }
    Return 0;
}
Four-instance variable operation


The instance variable is initialized with only a few settings, and later it needs to be set.



Instance variables distinguish visibility, a total of three public, protected, private, the default visibility is protected.



@public: Instance variable accesses the decorated symbol (public), the instance variable that is modified by the publicly, and can be accessed directly using the


#import <Foundation/Foundation.h>
#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(@"sex: %@", personOne->_sex);
         NSLog(@"age: %ld", personOne->_age);

         [personOne info];
     }
     Return 0;
} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Objective-c Learning Notes _ Classes and objects


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.