"Learning the path to iOS: Object-c" Classes and objects

Source: Internet
Author: User

1. Object-oriented and process-oriented concepts

1). Process oriented

Process-oriented: Take the process as the core, focus on the completion of the detailed steps of the event, step by step how to achieve.

2). Object-Oriented

Object-oriented: Taking things as the core, attention is the function that the thing that participates in this event should have. So completing this event is just one feature in all of the things.


2. Classes and objects

Class: A class is an abstraction of something that has the same characteristics and behavior, and it is an abstract concept, not specific.

Object: An instance of a class. The concrete embodiment of the class. All things in life are objects.

3.OO and OOP

OO: (object oritented) oo

OOP: (Object Oriented programming)


[Email protected] is OC flag

NSLog (@ "%hello world");//The purpose of the output is to verify.


5. How to create an object

1. Open space (heap area)

Example: Person *p = [person alloc];//open space, + number method, class method.

+ (ID) alloc; The ID is equal to void *, generic, and can represent all objects.

Because P stores the address of the object's heap space, all indirectly refers to p as the object, but essentially the pointer variable.


2. Initialization

That is: p = [P init]; Initialize, nil null 0


Method invocation form in 6.OC: message sending mechanism

[Receiver message];

1.+ Method Class method

2.-Method Object Method (instance method)

For example:

  Car *car = [[Car alloc] init];  [Car run];//run (); function

7. Definition of Class
* The definition of a class is divided into two parts: 1. Interface section (in. h file) 2. Implementing Department (in. m files)
* Interface section: Start with @interface + type: (colon means inherit) parent class name @end as End
* The contents of the interface section of the class must be written between the @interface and the @end.
* The implementation of the class is mainly the implementation of the method.
* The interface section of the class provides a declaration of instance variables and methods
The essence of a class is a custom data type.

For example: 1). Student.h declaration File

@interface student:nsobject{    @public        nsstring *_name;//name    nsinteger _age;//age    Nsinteger _num;//School Number       nsstring *_glass;//class    //instance variables must be written inside curly braces, and only instance variables can be written inside curly braces. Characteristics of the corresponding class, instance variables (struct members equivalent to struct)}-(void) study;//method name: study-(void) message: (NSString *) name1;//no return value, with one argument, parameter type NSString * Type//Method name sumvaluewitha:withb:withc:-(Nsinteger) Sumvaluewitha: (Nsinteger) a WITHB: (Nsinteger) b withc: (NSInteger) C; -(Nsinteger) Maxgbwitha: (Nsinteger) a WITHB: (Nsinteger) b;//method name MAXGBWITHA:WITHB: @end

2). STUDENT.M implementation file.
* Implementation part of the class: start with @implementation + Implement the class name @end as the end of the implementation part.
* Class implementation code must be unloaded between the @implementation and @end to be effective.


@implementation student-(void) Study {    NSLog (@ "Learn to go!");} -(Nsinteger) Moneyvaluewitha: (Nsinteger) a WITHB: (Nsinteger) b {        return a * b;} -(void) message: (NSString *) name1 {    NSString *s = name1;       NSLog (@ "%@", s);}     Least common multiple-(Nsinteger) Maxgbwitha: (Nsinteger) a WITHB: (Nsinteger) b {//store a B value nsinteger Tempa = A;     Nsinteger TEMPB = b;    Nsinteger rem = a% B;        while (REM! = 0) {a = B;        b = rem;    rem = a% B; } return (Tempa * TEMPB)/b;} @end
3). Main function implementation.

        Student *stu = [[Student alloc] init];      [Stu Study];      Nsinteger Mon = [stu moneyvaluewitha:10 withb:20];      NSLog (@ "%ld", mon);      [Stu message:@ "asdf"];

Note:
Code specification:
1. Class Name: English word composition, the first letter of each word capital, cannot appear the number, more cannot appear pinyin.
2. Instance variables: There are English words, except for the first letter lowercase, other words in the first letter uppercase.
3. In general, a class is defined in a one-to, H, and M file.

"Learning the path to iOS: Object-c" Classes and objects

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.