"iOS development" "oc"02-classes and objects

Source: Internet
Author: User

First, object-oriented

OC language is object-oriented, the C language is process-oriented, object-oriented and process-oriented are just two ways to solve the problem, the process is concerned with the steps involved in solving the problem, object-oriented is to design a class that can achieve the functions needed to solve the problem.

Terminology: Oo Object-oriented, OOP OO programming

II. Classes and objects

(i) About class

Class is designed to focus on only three things: class names, properties, and methods

Note: General nouns are classes, objects with the same properties and behavior can be abstracted as a class, the class name is one of the identifiers, need to conform to the specification, usually the first letter of the class name is capitalized, and cannot have an underscore, if there are more than one word use hump identification. In the classification of methods, the general practice is who is most familiar with this method and then divides this method to WHO. In OC, the invocation of an object to a method is called a message mechanism, that is, what message is sent to the given object.

(b) Simple memory analysis

class to create objects, each object occupies a certain amount of storage space in memory, each object has a separate member variable belonging to its own, all of the object common class member methods, methods in the whole memory only one copy, the class itself in memory occupies a portion of storage space, the method of the class is stored here.

Within each object, there is a default ISA pointer to the class used by this object.

[P eat]; indicates that a eat message is sent to the object that P points to, calling the Eat method of the object, where the object will follow the internal ISA pointer to find the method stored in the class.

ISA is a hidden pointer in an object that points to the class that created the object.

(iii) Declaration and implementation of the class

1) class declaration: Declare a car class, which has wheels and speed two properties, a Run method

  2) The implementation of the class:

  3) Class Call: First in the main function created p and P2 two car types of objects, the P object's wheels and speed properties are set to 5 and 250,p and P2 respectively call the Run method, the run wheels and speed properties set to 4 and 300.

    

  The output is:

    

  Exercise: Designing a Person class

    

  Output Result:

    

(d) Common mistakes

1. Declarations and implementations of classes cannot be nested

2. The implementation of the method must be placed in the @implementation, not in the @interface.

3. The method declaration cannot be placed in curly braces {}.

4. When declaring a new class, it cannot be nested with an existing class.

5. It is not permissible to assign a value (initialization) to a member variable in the @interface curly braces, or to use a member variable as a variable in the C language, as with static modification.

6. The declaration of the class must precede the main function.

Iii. Methods and functions

The difference between a method and a function:

(1) the declaration of the object method must be written between @interface and @end, and the implementation of the object method must be written between @implementation and @end

(2) The object method begins with the-number, and the class method starts with the + sign

(3) An object method can only be called by an object, and a class method can only be called by a class and cannot be called as a function

(4) functions can be written anywhere in the file (except between @interface and @end), and the function is owned by the file.

(5) Object method collation \ Object All

(6) The function call does not depend on the object

(7) The member variable of an object cannot be accessed directly from the member variable name within the function

Iv. design of classes and methods

Tool class: Basically there is no member variable, the method is basically the class method.

Note: You can call a class method in an object method.

Requirements: Design a tool class: A calculator class that requires (1) to return π, (2) calculates the sum of two integers, and (3) calculates the square of an integer.

  

Output Result:

    

"iOS development" "oc"02-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.