1. Duplicate member variables with member functions
Data attributes with the same name overwrite the method attributes. To avoid possible name conflictsProgramWhich may be hard to find
Bug-it is best to avoid conflicts with some naming conventions. Optional conventions include uppercase letters of the method and lowercase letters of the data attribute name prefix (
It may be an underline), or the method uses a verb while the data attribute uses a noun.
Class test ():
Def _ init _ (Self ):
Self. A = 'OK'
Def A (Self ):
Print ('test ')
A = test ()
Print (A. a) # OK
2. Importance of naming conventions
Users should be careful when using data properties-The user may modify the data properties at will, thus undermining the Data Consistency originally maintained by the method.
. Note that you can add data attributes to the instance without affecting the method to avoid naming conflicts.
Validity-once again, naming conventions can save a lot of trouble.
It is best to set the read attribute of a member through the member function.
3. Private variable definition
The conventions of private variables start with less double underscores and end with at most single underscores.
Of course, you can use various methods to access private variables (for example, if the derived class and the base class have the same name, you can use the private variable of the base class.
Quantity .), Do not do this unless you know what you are doing.
#-*-Coding: UTF-8 -*-
Class test ():
Def _ FUNC (Self ):
Print ('test ')
A = test ()
A. _ FUNC () # variables that may not be accessed
4. Implement the Organization bodies in C language through classes
John = employee () # create an empty employee record
# Fill the fields of the record
John. Name = 'John Doe'
John. Dept = 'computer lab'
John. Salary = 1000