Do not repost the labor results of developers
Some key concepts of object-oriented programming are mainly focused on syntax related to objective-c Definition classes.
The OC object-oriented concept is similar to that of other languages. It is closer to C ++ than other languages.
An object is an object. Object-Oriented programming can be seen as an object and what you want to do with it.
Object (noun) ---- implementation (verb), first defining class, then defining method ).
C language is a typical process-oriented language.
In C language, we usually consider what to implement first and then focus on objects. This is almost always the opposite of the object-oriented thinking process.
Implementation (verb) ---- object (noun). Define the function first, and then define the module ).
The unique existence of a class is an instance. operations performed on an instance are called methods.
In some cases, a method can be applied to an instance or a class.
Object usage can affect the object status.
Key concept: an object is a unique expression of a class. Each object contains information (data) that is usually private to the object ). Methods to access and change the data.
Objective-C uses the specific syntax to apply classes and instances:
[Classorinstance method];
When a class or instance is requested to execute an operation, a message is sent to it. The receiver of the message is called the receiver. Therefore, you can use another method to describe:
[Receiver message];
The method operation of objective-C is executed. One is to send messages, and the other is to call methods.
The previous idea is closer to OC.
The program is logically divided into the following three parts:
@ Interface
@ Implementation
Program
@ Interface is used to describe classes and classes;
@ Implementation is used to describe data (data stored in instance variables of class objects) and implement the actual code to declare methods in the interface;
The program code in the program section achieves the intended purpose of the program.
The common format of @ interface is as follows:
@ Interface newclassname: parentclassname
Propertyandmethoddeclarations;
@ End
According to the Conventions, the class name starts with an uppercase letter.
Name of instance variables, objects, and methods, usually starting with a lowercase letter.
When determining the name, follow the steps below to find the name that reflects the usage intent of the variable or object.
The program has a stronger self-explanatory)
Naming rules are quite simple: names must begin with letters or underscores (_), and can be followed by a combination of uppercase and lowercase letters, underscores, or numbers.
In addition, such as $ space, are invalid. Remember not to start with a number or use reserved words.
Again, objective-C is case sensitive. Sum, sum, and sum indicate different variables.
Class or instance method, with a negative sign (-) indicating the instance method, and a positive sign (+) indicating the class method.
The return type is placed in the parentheses after the negative or positive signs.
If a parameter exists, add a colon (:) after the method name, and add the parameter type and parameter name.
The specific example is as follows:
-(INT) currentage;
-(Void) print;
-(Void) setnumber: (INT) N;
Method Type (return type) method name method has parameter type parameter name
@ Implementation:
@ Inplementation newclassname
{
Memberdeclarations;
}
Methoddefinitions;
@ End
Note that the @ synthesize command can automatically generate some methods for the compiler.
Alloc is the abbreviation of allocate. If an alloc message is sent to a class, a new instance of the class is obtained.
This alloc method inherits from the parent class. The alloc method ensures that all instances of the object are in the initial state.
Of course, you must re-Initialize and call the init method when you want an appropriate method.
It is often the combination of alloc and init, or the new method is used directly.
For example:
Fraction * myfraction; myfraction = [fraction alloc]; // class method myfraction = [myfraction init]; // instance method fraction * myfraction = [[fraction alloc] init]; // two-in-one fraction * myfraction = [fraction new]; // class method, new includes alloc and init
Remember, the context of method execution is the object that receives the message.
The get and set methods are called access methods (accessor ).
This is the principle of data encapsulation. You can use this method to access data hidden from the "outside.
Use a program of the class to end this chapter:
//// Main. M // 3_2_class_object_method // created by haomengzhu on 14-11-01. // copyright (c) 2014 haomengzhu. all rights reserved. // # import <Foundation/Foundation. h> // -------- @ interface section ---------- @ interface fraction: nsobject-(void) print;-(void) setnumerator: (INT) N;-(void) setdenominator: (INT) d; @ end // -------- @ implementation section ---------- @ implementation fraction {int numerator; Int denominator;}-(void) print {nslog (@ "% I/% I", numerator, denominator);}-(void) setnumerator :( INT) n {Numerator = n ;}- (void) setdenominator :( INT) d {Denominator = D ;}@ end // -------- program section ---------- int main (INT argc, const char * argv []) {@ autoreleasepool {fraction * myfraction; // new object myfraction = [fraction alloc]; myfraction = [myfraction init]; // set [myfraction setnumerator: 1]; [Myfraction setdenominator: 3]; // print all nslog (@ "Hello, world! "); [Myfraction print];} return 0 ;}
Objective-C: Magic path [3-class, object, and method]