---------------------- ASP. NET + unity development,. Net training, and hope to communicate with you! ----------------------
Three main characteristics of object-oriented (essence): member variables, methods) encapsulation, inheritance, and polymorphism.
I. Encapsulation
1. Why is encapsulation required?
Based on what I learned previously, If I declare a student class, there is a public member age .. I wrote the code student * s = [studentnew] in the main function. In this way, I can modify the value of age through S → age, but the value of S → age =-10 will appear, but we all know that age cannot be-10 years old, we have no way to assign such unreasonable values to resistance values. We changed the method for accessing member variables so that this method can filter out unreasonable values.
@ Interface is like a clock surface exposed to the outside, like external display and interface. @ Implementation is like hiding the internal construction implementation of the clock and encapsulating the specific implementation.
2. Set Method:
In the development process, considering the security requirements, we generally do not use @ public, @ protected, or other keywords before the member variable name, but use the set method to provide the value of the member variable for the object. Some unreasonable values can also be filtered and filtered within the set method.
The role of the Set Method: provides a method for setting the value of member variables.
Naming rules:
(1) The method name must start with set.
(2) Keep up with the name of the member variable after the set, with uppercase letters
(3) the return value must be void.
(4) be sure to receive a parameter, and the parameter type must be consistent with that of the member variable.
(5) The name of the parameter cannot be the same as the variable name of the member. (The variable name of the member recommended by Apple should be prefixed)
Benefits of the Set Method:
(1) Prevent data from being exposed, ensuring data security
(2) filter the Set Data
Example of using the set method:
Set Method declaration:
Implementation of the Set Method:
Main function:
3. Get method:
Get method: returns the member variables in the object for the caller.
Naming rules:
(1) There must be a return value. The type of the returned value is the same as that of the member variable.
(2) The method name is the same as the member variable name.
(3) No parameters need to be received
Get method example:
Get method declaration:
Get method implementation:
Main function:
NOTE 1: In actual development, not all set and get methods are provided. If the internal member variables such as the student's student ID are only allowed to be read, if modification is not allowed, only the get method is provided instead of the set method.
Note 2: The name of a member variable starts with an underscore. The get method name does not need to be underlined. There are two advantages: (1) distinguish it from the method name of the get method; (2) You can distinguish some other local variables. Variables starting with an underscore are usually member variables of the class.
4. Advantages of encapsulation:
Hiding details and internal members ensures data security. Sometimes the Set Method and get method are not provided. For example, if a read-only member variable exists in the member variable, you only need to provide the get method, but not the set method.
6. Naming rules for member variables: Start. Benefits:
1> observe 4. In the example, the get method name is the same as the member variable name, which is easy to confuse. You can distinguish between the two by adding an underscore.
2> it can be separated from local variables. When you see variables starting with an underscore, they are generally member variables for easy communication.
Ii. Inheritance
1. Why inherit?
Assume that there is a class dog and a class cat. Their member variables are age and weight. The methods are the same. The declaration defined in the two classes is very wordy, and many things are the same. We can extract these identical things to form a class animal, and then use the inheritance function to make dog and cat inherit the animal class to have all the contents of animal. In this example, dog and cat are subclasses of animal, and animal is the parent class of cat and dog. This feature simplifies a large amount of code.
2. Benefits of inheritance:
1> duplicate code extraction
2> established a class relationship.
3> subclass can have all member variables and methods in the parent class
3. Note: basically, the root class of all classes is nsobject, new comes from it, and new is the class method.
4. Notes for using inheritance:
1> the parent class must be placed before the subclass.
2> The subclass and parent classes are not allowed to have member variables with the same name.
3> when a method is called, it is first found in the current class. If no method is found, it is found in the parent class.
5. Rewrite: The subclass re-implements a method in the parent class, called rewrite, which can overwrite the previous practice of the parent class.
6. disadvantages of inheritance: Strong Code Coupling
7. Differences between combination and inheritance: inheritance is XX (student is person), and combination is XX (student has score ).
8. Keyword super:
1> like self, the @ keyword is not required.
2> directly call a method in the parent class
3> If super is in the object method, the parent class Object method is called. If super is in the class method, the parent class method is called.
3> A common use case is that the behavior of the parent class is reserved when the child class overrides the parent class method.
Iii. Polymorphism
1. polymorphism essence: the parent class Pointer Points to the subclass object and there is no polymorphism without inheritance.
2. Benefits of Polymorphism
On the basis of 2, design a cat class to inherit animal, override the Eat method, output "cat eat ----", and then design a function feed function as follows:
void feed (dog*d){ [d eat];}
This function can be used to execute the action of eating a dog, but cannot perform the action of eating a cat. If you want to call the main function to make cat eat, you need to design a function as follows:
void feed2 (cat*c){ [c eat];}
Write the following code in the main function:
Dog *dd = [Dognew];Cat *cc = [Catnew];feed(dd);feed(cc);
In this way, cats and dogs can be successfully called to eat.
3. Limitations of polymorphism:
If the dog class in the above example adds an object method run, execute the following code:
Animal *a = [Dognew];[a run];
When the above code is executed, a warning is generated but it can be run. We do not want to write it like this. This is the limitation of polymorphism: the pointer of the parent class cannot be used to call the special method of the subclass. If you really want to call it, you can use the forced conversion method. The Code is as follows:
Animal *a = [Dognew];Dog *d = [Dog *]a;[d run];
That is, it must be converted to a subclass type pointer variable.
---------------------- ASP. NET + unity development,. Net training, and hope to communicate with you! ----------------------
For details, see www.itheima.com.