Object-oriented features:
1) package 2) inherit 3) polymorphic
Knowledge points :
(1) Set/get method (encapsulation of member variables)
(2) Construction method
(3) Use of self
(4) Object methods and class methods
Small Knowledge Point Supplement
(i)Set/get method
"Set": Assigning a value to a member variable with a formal parameter
"Get": Returns the value of a member variable
code example
#import<Foundation/Foundation.h>//Statement@interfacecar:nsobject{//@public//the next member variable is best not to write @public,//because @public modified member variables can be changed by others int_wheels;//Number of wheels naming conventions for//member variables //member variables begin with the underscore _//1. Can be separated from the name of the Get method//2. A variable that looks at the beginning of an underscore is definitely a member variable
/* Set Method 1. Function: Used to set the member variable, you can filter out some unreasonable values in the method 2. Naming specification: The 1> method starts with a set and follows the member variable name, and the first letter of the member variable name must be capitalized 2> parameter name do not The member variable has the same name as 3. Because only the method setting value is provided, the return value is void*/- (void) Setwheels: (int) Newwheels;/*Get Method 1. Function: Returns the member variable within the Object 2. Naming specification: The name of the 1> get method is typically the same as the member variable 3. Because the Get method is to get the value of a member variable, the return value type is the same as the member variable type*/- (int) Wheels;//Run- (void) run;@end@implementationCar//the realization of running- (void) run{NSLog (@"%i A Wheel of cars running! ", wheels);}//implementation of Set method- (void) Setwheels: (int) newwheels{//filter the number of wheels that come in outside (when we need to filter the data) if(newwheels<=0) {Newwheels=1; } _wheels= Newwheels;//Just write this sentence.}//implementation of the Get method- (int) Wheels {return_wheels;}@endintMain () {Car*car = [carNew]; // car->wheels = -10; You can use this method to set a value directly when @public//set the _wheels member variable of a car object by using the Set method[Car setwheels:- -]; [Car Run]; //[car wheels] Get _wheels member variable of car object by get methodNSLog (@"Number of wheels:%i", [car wheels]); return 0;}
Add
// multiple -( void ) Setage: (int ) a andweight: (int ) W andheight: (int ) h{ // method One: Assign a value directly to a member variable // _age = A; // _weight = W; // _height = h; // method Two: The setter method for encapsulating member variables [Self setage:a]; // self = = Wangcai [self setweight:w]; [Self setheight:h]; }
(ii) Construction method
(Initialize object)
1. There are two types of construction methods
(1) Default construction method provided by the system
(2) Custom construction method
2.
> The method that the system automatically calls when creating objects or variables in the general language is called the construction method (this concept is OK, the focus is below)
Unfortunately, OC does not automatically call the construction method when creating an object
> How the Constructor works: Initializes the object's member variables when the object is created
>OC, any method that starts with Init is a construction method
> construction method is divided into non-parametric construction method, and the method of constructing with parameters
A class can have more than one construction method, and when creating an object, it chooses to call the construction method according to the requirement.
3.1 "Non-parametric construction method"
Declared format:-(pointer to the current class type *) init;
The implemented format:
-(Dog *) init{
Self = [super Init];
if (self!= nil) {
_name = @ "Tom";
_age = 0;
}
return self;
}
"Note": A complete object is divided into the parent class and the current class two parts, the initialization time to
1) first go through the Super keyword first to initialize the parent class this part, the initialization of the parent operation return value assigned to self
2) to determine the return value when initializing the parent class, if the return value is not NULL, the parent class initialization succeeds to initialize the current class-specific member variable
3) return to self
3.2 "With parameter construction method"
In fact, with a parameter ~ ~ So easy ~ ~ Reference to the Set method with multi-parameter
3.3 "Initialization Order of objects"
1) first go through the Super keyword first to initialize the parent class this part, the initialization of the parent operation return value assigned to self
2) to determine the return value when initializing the parent class, if the return value is not NULL, the parent class initialization succeeds to initialize the current class-specific member variable
======
(iii) use of self
1)
Self: The object or class that accepts the message, and a pointer to the current object (to the method caller ). Note: in the class method, self must point to the class
Super: The parent class of the object that accepts the message, which is used to invoke the Member method of the parent class.
The place of occurrence : all the OC Method (Object method \ class method), cannot appear in the function
2) function
1> to access member variables of the current method call using the self-> member variable name
2> using "[Self method name ];" To invoke Method (object method \ class method)
code example:
/*self 1. Usage: can only be used in methods (object method \ class method), cannot be used in function 2. Each time the method is called, the system automatically creates the auto pointer 3.self pointer to the method caller*/#import<Foundation/Foundation.h>@interfacecar:nsobject{int_wheels;}- (void) Setwheels: (int) Wheels;- (int) Wheels;- (void) run;@end@implementationCar//all member variables, whether @public or not, can be accessed directly in the object method- (void) Setwheels: (int) wheels{ Self->_wheels =Wheels;}- (int) wheels{returnSelf->_wheels;}- (void) run{//Self is a special pointer, only in the @implementation method.//Self is actually a built-in pointer to the method, and each time the method is called, there will be a self pointer//The self pointer points to the method caller int_wheels =Ten; //Use the self pointer to access the member variable of the method call _wheelsNSLog (@"%i A wheel of a car! ", self->_wheels);}/*error notation: Self cannot be used in a function void Test () {self->_wheels = ten;}*/@endintMain () {Car*c = [CarNew]; [C Setwheels:4]; [C Run]; return 0;}
(iv) Object methods and class methods
> Object Methods
1) with minus -start
2) Only the object can be called, no object, this method cannot be executed at all
3) Object methods can access instance variables (member variables)
> class Methods
1) Start with a plus + +
2) can only be called with the class name (directly called through the class name), the object cannot be called
3) instance variables (member variables) cannot be accessed in a class method
4) Application: When you do not need to access member variables, try to use the class method
Note: Class methods and Object methods can have the same name, and instance variable _xxx cannot be accessed in class methods.
three functions of the > "class method"
1: Simple and fast object creation
2: If a function is accomplished by multiple methods, a simple and easy-to-use interface can be set up using the class method
3: Implement a singleton mode
===============
Small Knowledge point supplement:
(1)
Nil: If you want to assign an object to null with nil
Nil: If you want to assign the base data type to null with nil
void * : variable that can point to any data type
ID Type : You can reference an object that is instantiated by any class and does not require a strong turn when sending messages to the referenced object
"Self and Super keywords"
Self: The object or class that accepts the message, the pointer to the current object (which is used by that object to whom Self,self is).
Super: The parent class of the object that accepts the message, which is used to invoke the Member method of the parent class .
(2) Memory representation of an object
Member variables of the parent class
Member methods of the parent class
member variables of their own
Member methods of their own
(3) Compound object
Putting elephants into refrigerators with object-oriented thinking
Object member variable member function class
great elephant age/weight/height setter/getter Elephant
Ice Box Elephant * ele opendoor/storeelephant/closedor Haier
oc--classes and objects for iOS development (2)-set/get method and construction method