------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
Naming conventions for member variables:
Member variable name to begin with _
Role:
1. You can leave the name of the member variable and the Get method separate
2. Can be separated from the local variables, the first to see the variables beginning with _ are usually member variables
Packaging:
When defining member variables, try not to use the @public notation, but instead use the Set method and the Get method
@interface student:nsobject{int _age;}
/* Set Method: function: Provide a method to set the value of the member variables, you can filter the parameters of the name specification: set at the beginning of the name of the member variable, the first letter of the member variable capital return value is (void) must accept a parameter, The type of the parameter and the member variable type have the same name as the member variable name */-(void) Setage: (int) age
/* Get Method function: Returns the member variable naming specification inside the object: there must be a return value, the return value type must be the same as the member variable type the method name does not need to receive any parameters like the member variable name */-(int) age; @end
intnew]; [Stu Setage: Ten // call the Set method to set the value of a member variable // Call the Get method to read the value of the member variable return0;}
If the internal member variable system cannot be modified, you only need to provide a Get method
Object methods
1> minus-Start
2> can only be called by an object
A member variable (instance variable) that can access the current object in the 3> object method
Class method
Class methods start with A +,
Can only be called by a class.
Instance variables (member variables) cannot be accessed in a class method
class method call class method will die loop
+ (void) test{nslog (@ "%33"// will always output 33 dead loops
Benefits and use occasions:
1> is not dependent on the object, the execution efficiency is high
2> can use class method as far as possible
3> when you do not need to use a member variable inside a method, you can use the class method
Object methods
Object methods begin with-
Object methods can only be called by objects
can access member variables of the current object
Object methods can call class methods
Allow class methods and object methods to have the same name
Tool class: Basically does not contain any member variables, the method is basically a class method, you can call the previous class method
Self keyword
Self is a pointer to the object that invokes the current method
Who calls the current method, self represents who
Self appears in the object method, and self represents the object
Self appears in the class method, and self represents the class
Self use
Member variable names inside the current object can be accessed through the self-> member variable name in the 1> object method
2> [self method name]; [Self bark]; Other object methods and class methods can be called
3> who calls the current method, self represents who
* Self appears in the object method, and self represents the object
* Self appears in the class method, and self represents the class
2> accessing member variables inside the current object using the "self-> member variable name" in the object method
Self, pay attention.
1. To clearly understand whether the object or class is calling self
2.self cannot call a function method
Inherited
Is the relationship between classes and classes
1. Benefits of Inheritance:
1> Extracting Duplicate code
2> establishes a relationship between classes
3> subclasses can have all member variables and methods in the parent class
2. Attention points
1> basically the root class of all classes is NSObject
2> the parent class must be declared before the child class
The 3> subclass cannot have the same member variables as the parent class
4> call a method, first go to the current class to find, if not found, go to the parent class to find
Overriding: Subclasses re-implements a method in the parent class, overwriting the previous practice of the parent class
The declaration is not implemented in the parent class, and it is possible to implement it in a subclass
Use occasions
When two classes have the same properties and methods, you can extract the same things into a parent class
When Class A fully owns some of the properties and methods in class B, consider letting Class B inherit Class A
Cons: Coupling is too strong
//Animal's statement@interfaceanimal:nsobject{int_age;}- (void) Setage: (int) age;- (int) age;@end//The realization of animal@implementationAnimal- (void) Setage: (int) Age{_age=Age ;}- (int) age{return_age;}@end//define a dog class//: Animal inherits Animal, equivalent to having all member variables and methods in Animal//Animal called Dog's parent class//a subclass of dog called Animal@interfaceDog:animal@end@implementationDog@endintMain () {Dog*d = [DogNew]; [D setage:Ten]; NSLog (@"age=%d", [D age]);return 0;}
The role of the keyword super
1. Call a method in the parent class directly
2.super is in the object method, the object method of the parent class is called
Super is in a class method, then the class method of the parent class is called
3. Usage scenarios: Subclasses overriding the parent class's methods when they want to preserve some behavior of the parent class
, which is the extension of subclasses based on the parent class.
*/
Polymorphic
Only the inheritance is polymorphic.
Polymorphic
No inheritance, no polymorphism.
The embodiment of the code: a pointer to the parent class type points to the subclass object
Benefit: If you are using a parent class type in a function \ method parameter, you can pass in the parent class, the child class object
Limitations:
A variable of a parent class type cannot directly invoke a method that is unique to a subclass. You must strongly convert to a subclass type variable to directly invoke a method that is specific to a subclass class
*/
The package inheritance and polymorphism of Dark Horse programmer--oc Class