IOS First Day

Source: Internet
Author: User
Tags greatest common divisor

First, initial classes and objects

A superset of C language that allows the use of C language source code in OC. compiler compatible with C language programs

With perfect object-oriented features

Contains a run-time system

Class Library Rich

Oop for object-oriented programming

Object-oriented language: C + + java C #

Concepts: objects, classes, encapsulation, inheritance, polymorphism, etc.

Process-oriented: Analyze the steps to solve the problem, implement the function, and sequentially call

Object-oriented: Analyze the object of the problem, coordinate the contact and communication between the objects, and solve the problem

Object-oriented design has good scalability and reusability

Classes and objects are the object-oriented core

Defining classes, creating objects, working with objects

Abstraction of something with the same characteristics and behavior

Object is an instance of a class

Class is the type of object

@ is the mark of OC

purpose of the output: verify

Using object completion programs in object-oriented programming

In development: Define the class first, create the object, and then use the object

The definition class consists of two parts: the interface part and the implementation part, which are written separately

1. Interface part: Characteristics and behavior of the foreign Declaration class

2. Implementation part: Internal implementation behavior

Interface section. h

Interface Part flag: @[email protected]

Function: Declares the instance variable and the? Method of the class, that is, the feature and the behavior.

Content: Class name, parent class name, instance variable, method, and so on

* Specification:

1. Class name : Composed of English words , the first letter of each word should be capitalized , can not appear Chinese characters and pinyin .

2. Instance variables : Method name : Consists of English words, except the first letter lowercase, the rest are uppercase ,

3. in general, a class is defined in a pair of . h and. m files.

NSObject is the only class in OC that does not have a parent class. called the root class.

* The definition of a class is divided into two parts : 1. Interface section (. h) 2. Implementation section (. m file )

Interface section: start with @interface + class name. : ( a colon means inheritance) after a colon is the parent class name. @end as the end.

The contents of the interface portion of the class must be written between the @interface and the @end .

The interface section of the class provides an instance variable and a declaration of the method .

The nature of a class is a custom data type .

1 //interface for the person class2 @interfacePerson:nsobject3 {4     @public5     //instance variables must be written in curly braces, and only instance variables can be written in curly braces.6     //characteristics of the corresponding class: instance variables (struct members equivalent to struct bodies)7     //String Type8NSString *name;//name9NSString *gender;//SexTen     //Integral type OneNsinteger age;//Age A     //floating Point Type -CGFloat height;//Height - } the //Behavior---method (function) for-------class - //void SayHello (); ---c-language functions -- (void) SayHello;//Oc Method Method Name: SayHello no return value no participation -  + //int Getmoney (); ---C language functions --(Nsinteger) Getmoney;//Oc Method Method Name: Getmoney has return value no participation +  A //void Outputp (int n); ---c-language functions at- (void) Output: (Nsinteger) n;//Oc Method Method Name: Output: No return value has parameter -  - //int Sumvalue (int a, int b, int c); ---c-language functions --(Nsinteger) Sumvaluewitha: (Nsinteger) a B: (Nsinteger) b C: (Nsinteger) C; -   //method Name: sumvaluewitha:b:c: -  in  --(Nsinteger) Mingbwitha: (Nsinteger) a B: (Nsinteger) b;//Beg least common multiple to    //method Name: mingbwitha:b: +  - @en 

Implementation section. m

Implementation Part flag: @[email protected]

Function: The implementation method is the implementation of the class behavior.

1 #import "Person.h"2 /**3 * Implementation part of Class: Start with @implementation + implement class name. 4 @end as the end5 the implementation code of the class must be written between the @implementation and the @end.6 The implementation of the class is mainly the implementation of the method7  */8 @implementation Person9- (void) SayHello {TenNSLog (@"Hello, Beautiful"); One } A  --(Nsinteger) Getmoney { -     return  -; the } -  --(void) Output: (Nsinteger) n { -NSLog (@"n =%ld", n); + } -  +-(Nsinteger) Sumvaluewitha: (Nsinteger) a B: (Nsinteger) b C: (Nsinteger) C { A         returnA + B +C; at  - } -  --(Nsinteger) Mingbwitha: (Nsinteger) a B: (Nsinteger) b { -     //1. Storing the value of a B -Nsinteger Tempa =A; inNsinteger TEMPB =b; -     //2. Seeking greatest common divisor toNsinteger temp = a%b; +      while(Temp! =0) { -A =b; theb =temp; *temp = a%b; $     }Panax Notoginseng     returnTempa * TEMPB/b; - } the  + @end

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 .

Object-oriented : Taking things as the core , paying attention to the function of the thing that participates in the event , and accomplishing the event is just a function of all the functions of the thing .

* OO: (object oriented) Oo .

OOP: (Object Oriented Programming) faces object programming.

Class : An abstraction of something with the same characteristics and behavior, which is an abstract concept , not specific .

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

How do I create an object from a class ?

There are two steps to creating an object : 1. Open Space (heap area ) 2. Initialize

The invocation form of the method in OC.

message delivery mechanism [receiver message]

ID equals void *, generic, can represent all objects

Main.m

   Person *per = [[Person alloc] init];        [per SayHello];         = [per Sumvaluewitha:ten B:5 C:4];        NSLog (@ "sum  =%ld", sum);         = [per Mingbwitha:ten B:6];        NSLog (@ "mingb =%ld", MINGB);        [Per output: 5];
  //Accessing instance variablesPerson *per =[[Person alloc] init]; Per->name =@"Frank"; Per->gender =@"m"; Per->age = -; Per->height = the; //the value of the output instance variableNSLog (@"name =%@", per->name); NSLog (@"gender =%@", per->gender); NSLog (@"Age =%ld", per->Age ); NSLog (@"height =%f", per->height);

Summary:

Classes and files

Class: @[email protected] @[email protected]

File:. h is called an Access port? file or header file,. m called implementation? file. The default settings are as follows:

1. Name the file with the class name?

2.. h. The connection port section of the file management class;. M. File Management class implementation Section

The file is not related to the nature of the class, in order to facilitate management and achieve encapsulation characteristics

Creating objects

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 an initial value for an instance variable of an object

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

Initialization: p = [P init]; Typically these two operations require ligatures:

person * p = [[Person alloc] init];

+ (ID) alloc; + table? The method belongs to a class and can only be used as a class. The ID returns a value type, and the table shows any type of object, that is, the created object.

-(ID) init; -table? The method belongs to the object and can only be used by the object. The ID returns a value type, a table that initializes the completed object.

initialize

-(ID) Initwithname: (NSString *) Name: (Nsinteger) Age    {//[self init] is to invoke    this class to implement the Init method //[Super Init] is to call the parent class on the    implementation of the Init method // If the subclass does not override the Init method, use the [Self init] is exactly the    same as [super init] // But if the subclass overrides the Init method, there is a difference.    Self = [super init];     if (self) {        = name;         = Age ;    }     return Self ;}

Working with objects

The pointer stores the first address of the object, substituting the object. The use of pointers to refer to objects in OC.

person * p = [Person alloc]; To the right of the first line "="

1. [Person Alloc] The return value is the first address of the object, that is, the object.

2. P is a pointer variable of the same type as the object, storing the object? First address, referring to the object

int main (int argc, const char * argv[])

{

Student *zhangsan = [[Student alloc] init];

[Zhangsan sleep];

Manipulating instance variables

Instance variables are initialized with only a small set of settings, and later need to be set into rows.

The instance variable can be divided into three kinds of visibility. This lesson makes it public @public: instance variables access the decorated symbols (publicly).

@interface lesson:nsobject{//@public// public, publicly modified variable. can be accessed in any File/ /    @protected// protected and can only be accessed in its own classes and subclasses    @private  // Private, can only access NSString *_name in their own class    ;   instance variables are underlined (unwritten) to differentiate other variables    //string  strings    Nsinteger _age;
Syntax details, precautions

L member variables cannot be initialized in {} and cannot be accessed directly

The L method cannot be called as a function

L member variable \ Method cannot be modified with keywords such as static, do not mix with C language (temporarily ignore)

The implementation of the L class can be written behind the main function, mainly after the declaration.

L C function cannot access members of OC object

L method only declaration, no implementation (classic error)

The L method is not declared, only implemented (compiler warning, but can invoke, OC's weak syntax)

L Compile: Access no member variable directly error, Access no method, just warning

IOS First Day

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.