Dark Horse Programmer--Object oriented
-------Android Training, iOS training, looking forward to communicating with you! ----------
class creation and use * * * *
. h: A declaration file for a class that declares member variables, methods. class declaration using the keyword @interface and @end
. m: The implementation file for the class that implements the method declared in. h. Classes are implemented using keywords @implementaion and @end
Method:
The Declaration and implementation of a method must begin with a "+" "-"
+ denotes class method (static method)
-Represents an object method (dynamic method)
All method scopes declared in. h are of the public type and cannot be changed
Member variable (instance variable \ Property):
@public can be accessed globally
@protected can only be accessed within the class and within subclasses
@private can only be accessed within the class
The definition of a class is divided into two parts
1) class declaration (Specifies the class name, attribute, behavior) of the current class:
@interface Class Name: Parent class name (NSObject)
{
Defining the properties of a class
}
Behavior of the class
@end
2) Implementation of the Class (Implementation specific behavior)
@implementation class Name
The specific implementation of the behavior
@end
Method implementation: First copy the method declaration, and then add a curly brace, write the implementation code inside the method, if you want to access the current object's member variables, directly write the variable name can be.
*/
1 //creates a class and creates an object, and accesses the value of the storage space with the object2 3 /*Create a new object: Car *car=[car new];*/4 5#import <Foundation/Foundation.h>6 7 //Declaration of the class of the vehicle8 9 @interface Car:nsobjectTen One { A - //properties of the class - the @public; - - intLunzi;//the wheels of the car - +NSString *color;//the color of the car - + intSpeed//the speed of the car A at } - - //behavior of the class - - @end - in //implementation of the class - to @implementation Car + - //specific description of the behavior the * @end $ Panax Notoginseng intMain (intargcConst Char*argv[]) - the { + ANSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init]; the + //create an object of type car car1 - $Car *car1=[carNew]; $ - //create an object, in essence or pointer (indirectly accessing the value of an instance variable with a pointer) - theCar1->lunzi=3; - WuyiCar1->speed= Max; the -Car1->color=@"Blue"; Wu - //View the car's information About $NSLog (@"Wheel:%d, speed:%d, color:%@",car1->lunzi,car1->speed,car1->color); - - [Pool drain]; - A return 0; + the}
Creation of objects in/*OC
[Car new];
Did three things.
1. The computer requests the memory space
2. Initialize values for each member of a class
3. Return to the first address of the application space
Car *p=[car New];
Understand one:
Defines a pointer variable for a car type
Pointer variable points to the memory space of the new request
Understanding Two:
An instance object is instantiated with the car class, and the name of the object is P
*/
1 //creates a class and creates an object, and accesses the value of the storage space with the object2 3 /*Create a new object: Car *car=[car new];*/4 5#import <Foundation/Foundation.h>6 7 //Declaration of the class of the vehicle8 9 @interface Car:nsobjectTen One { A - //properties of the class - the @public; - - intLunzi;//the wheels of the car - +NSString *color;//the color of the car - + intSpeed//the speed of the car A at } - - //behavior of the class - - @end - in //implementation of the class - to @implementation Car + - //specific description of the behavior the * @end $ Panax Notoginseng intMain (intargcConst Char*argv[]) - the { + ANSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init]; the + //create an object of type car car1 - $Car *car1=[carNew]; $ - //create an object, in essence or pointer (indirectly accessing the value of an instance variable with a pointer) - theCar1->lunzi=3; - WuyiCar1->speed= Max; the -Car1->color=@"Blue"; Wu - //View the car's information About $NSLog (@"Wheel:%d, speed:%d, color:%@",car1->lunzi,car1->speed,car1->color); - - [Pool drain]; - A return 0; + the}
Parameterless method declaration and Invocation * * * *
Method type identifier (return value type) method name:
Methods with no parameters
Statement:
-(return value type) method name-(double) pi
Method invocation: [Class name or object name Method name];
Object methods differ from class methods * * * *
Object method:
1, the object method with "-" beginning, such as-(void) xx;
2, object methods can only have objects to explain
3. You can access the member variables of the current object in the object method
4. Call format [Object Name object method name];
Class method:
1, the class method with "+" beginning, such as + (void) xx;
2. Class methods can only be called by classes
3. Class methods cannot access instance (member) variables because class methods are called by the class and do not create storage space to store member variables in the class
4, call format: [Class name class method name];
5, the class method does not depend on the object, the execution efficiency is high, can use the class method as far as possible to use
*/
-------Android Training, iOS training, looking forward to communicating with you! ----------
Dark Horse Programmer--Object oriented