OBJECTIVE-C Classes and objects

Source: Internet
Author: User
Knowledge point

1. Preliminary understanding of OC

2.xcode use

3. Object-oriented programming ideas, classes and objects

4. Definition and use of classes

5. Class instantiation-object

6. Separation of declaration and implementation

 

===============================================

1. What needs to be understood (theoretical part)

 Part I: OC and Object Orientation

1.1. History, characteristics and importance of OC language

[What is OC]

 

Objective-C based on C language, object-oriented language;

 

Pay attention to the difference between C language and oc language syntax;

 

 

IOS, iphone, ipad…

 

 

[Notes for entering the OC stage]

 

1. The difference between oc language and c language;

 

2. Type the code more and debug the program patiently;

 

3. Familiar with OC syntax and object-oriented thinking through several data model projects

 

4. The class provides a large number of methods to summarize the accumulated habits;

 

 

======================================================= ===================

How to create an OC project in Xcode

 

 

[Common shortcut keys]

 

[Compile] command + b

[Run] command + r

[Show and hide the debug box and print box] command + shift + y

[Stop] command +.

[Comment] command + /

[Code alignment] control + i

 

Other shortcut keys:

 

 

Spend: command, ^: control, ??: shift

 

 

 

 

2.2 OC is compatible with C, different from C language

Difference between various file extensions .c and .m and .mm

 

 

.c C source code asking price;

.m: C + OC file;

.mm: C ++ + OC file;

 

.h header file;

 

#importAction

 

Include the documents;

 

 

 

 

 

 

The difference between @ "hello" and C string

 

oc string: @ "string"

C string: "string"

 

 

The difference between NSLog function and printf function

 

// NSLog printf

// 1. Wrap by default

// 2. Expected to print OC string Expected to print c string

// 3. The project name and time are printed out. The project name and time are not printed out.

// 4, can print oc object% @ cannot print oc object

 

  [NSLog printing of various data types]

 

char 1 Byte

short 2 Byte

int 4 Byte

long 8 Byte

float 4 Byte

double 8 Byte

 

BOOL

NSInteger

NSUInteger

CGFloat

 

 

 

Exercise: Output the number of bytes of various data types

 

 

 

[Restore shortcut] command + z

 

 

 

 

================================================== ================================

 

3. Process-oriented and object-oriented:

 

C language is a process-oriented language;

 

OC language is an object-oriented language;

 

 

Process-oriented: What processes (steps) are needed to solve a problem;

 

 

Object-oriented: which objects are needed to solve a problem;

 

 

3.1, process-oriented and object-oriented

3.1.1 Process-oriented programming

[Process-oriented language]

1. Omit subject; (subject is cpu)

2. Process-oriented = data + algorithm;

3. Mathematical mapping;

 

 

【example】

Process-oriented

Elephants in refrigerators;

1. Open the refrigerator;

2. Put the elephant in the refrigerator;

3. Close the refrigerator;

 

Turn on the computer to play games;

1. Turn on the computer

2. Start playing the game;

3. Shut down the computer;

 

 

[Object-oriented language]

 

1. The subject is the object, the main factor is the object; each object can receive different information, and these work together to accomplish one thing;

2, object-oriented = object + message;

3. Life mapping;

 

Applications are written in object-oriented languages;

 

change:

【example】

Object-oriented

Refrigerator elephant (two objects: refrigerator, elephant)

1. Open the door of the refrigerator;

2. Refrigerator to store elephants;

3. The refrigerator closes the door;

 

 

Turn on the computer to play games:

1. The computer is turned on;

2, people play LOL

3. The computer is turned off;

 

 

Process-oriented: What processes (steps) are needed to solve a problem;

Object-oriented: what objects are needed to solve a problem;

 

For example:

1. Building a house; 1. Laying foundation; 2. Bricklaying; 3. Decoration;

 

1. Zhang San who can lay foundations; 2. Li Si who can lay bricks; 3. Pharaoh who can decorate

 

2. Connect to IOS project; 1. Write iOS code; 2. Meet art artist; 3. Will sell;

 

  1. Whole eggs that can write iOS code; 2. Small flowers that can be artistic; 3. Iron pillars that can be sold;

 

 

3.2 The concept of classes and objects

 

Class: A description of an object, which is a template for the object;

Objects: Objects are real, can be seen and touched, occupy storage space in memory, and are created in the same way;

 

 

I. Recognize classes and objects

 

Objects are the core of object-oriented programs. So how do you understand objects, what is the relationship between classes and objects.

 

7. The difference between a class and an instance

  1) A class is a template for an object.The class is static and does not occupy memory itself, it is only a description of something of a certain type.

  2) The object is the materialization of a template

 

3.2.1 From the logic of life

Class object

People Zhang San, Li Si, Lao Wang

Computer my desktop this computer, your desktop the computer;

Dog Xiaobai, Wangcai

Superheroes Iron Man, Hulk, Spider-Man;

 

3.2.2 From the perspective of packaging

Structure = data encapsulation;

Function = code wrapper;

Class = encapsulation of data and functions;

 

Function (method)

 

Three object-oriented characteristics: 1. Encapsulation; 2. Inheritance; 3. Polymorphism;

 

 

3.2.3 From the perspective of programming languages

The class is a custom type without occupying memory

Objects are variables created with classes. Variables defined with types take up memory

 

 

3.2.4 Official definition

Class: abstraction of the same class of things with the same characteristics;

Object: a variable of a certain class type

 

 

===============================================

4. Need to remember the master (the actual part)

 

[How to design a class]

 

Humanity:

Member variables: name, age;

Member methods (functions): eat, sleep, peas;

 

Most keywords in OC start with @;

 

Design a class in two parts: class declaration, class implementation;

 

4.1, [Declaration of Class]

 

// @ interface class name: parent class

  {

// Member variables

}

 

// declaration of member methods;

 

 

// end

 

4.2. [Class implementation]

// @ implementation class name

Implement member methods;

 

// @ end

     4.3, [Member Variables]

 

Describe what you have

 

     4.4 [Member Method]

 

Describe what can be done;

 

4.5, [How to create an object]

 

[Class name alloc];

 

 

4.6. [Method invocation]

 

[Object method name];

 

 

Requirements: access to member variables? Access member variables through methods:

 

 

 

Member method details:

1, no parameter, no return value member method-(void) method name;

2, with parameters-(void) method name: (parameter type) parameter name;

3. Bring back the worthwhile method-(NSUInteger) getAge;

4, method with multiple parameters-(void) Note: (parameter type 1) parameter name 1 label 2: (parameter type 2) parameter name 2;

 

 

 

 

Things about naming:

1. Member variable naming: start with _;

2. Method names and member variable names: The first word is lowercase, and the first letter is capitalized from the second word;

3. Class name: Capitalize the first letter of each word;

 

 

 

 

Exercise: [test1]

 

Write a student class:

Member variables: name, student number, weight;

Three methods of implementation: setting the name; setting the student number; setting the weight;

A way to achieve: At the same time can set the name, student number, weight

A way to achieve: learning;

Implement a method: Get the student number;

Implement a method: introduce yourself;

 

 

 

 

Note: What is an object and what is an object pointer?

 

Student * s = [Student alloc];

 

Object pointer: s in the stack area;

Object: [Student alloc]; In the heap area;

 

 

Object-oriented:

1. Find out the object first;

2. List the member variables and member methods required by the object;

3. Design category;

4. Create the object through the class;

5, let the object do things;

 

 

Exercise:

1. Write an adder that can receive two integers int and then output the sum of the two integers;

 

One object: adder;

 

Member variables required: int, int

Method: addition;

 

 

 

 

In the future, the creation of classes in the project must be that each class is in separate files .h and .m, and the class will not be created above the main function

 

 

.h is the class declaration;

.m is the implementation of the class;

 

 

Use this class (note) #import the class's .h header file;

 

#import <system-provided.h>

#import "Custom header file.h"

 

1. How to design a class;

2. The syntax of the member method;

3. How to create an object;

4. Call the member methods of the object;

 

 

 

Exercise:

 

2. There is a rectangular class with member variables: length, width, area, and perimeter;

  The methods are: setting length and width, printing area, printing circumference;

 

 

 

 

 

3.Design dogs

Attributes: name, age, weight

Method: say, sell salesBud;

Write three methods respectively and assign values to three member variables;

    Write another method to assign values to three member variables at the same time;

Write another method to print the values of three member variables;

 

 

 

 

 

 

 

【operation】

1. It is known that the speed of a car is 20Km / h. If it is driven over a distance of 1000km, how many hours will it take?

 

 

 

2. Create a highway, the attributes have length and width, and write the setting and value methods of length and width.

 

 

3. Xiaoming has two cards in his hand, a red heart A in his left hand and a square K in his right hand.

Q: What are the cards in my hand after the exchange?

 

objective-c class and object
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.