Facing objects in object (oop) is a very important knowledge point. We can simply regard it as a and methods used to access and operate the data. After learning functions, we know why to replace functions with classes if code is reused?
Class has such advantages
1) class objects are polymorphism: multiple forms, which means that we can use the same operation method for different class objects without the need to write additional code.
2) Encapsulation of classes: after encapsulation, you can directly call class objects to operate some internal class methods without allowing users to see the details of code work.
3) Inheritance of classes: classes can be directly used to inherit their methods from other classes or meta classes.
Definition class syntax
Copy codeThe Code is as follows:
>>> Class Iplaypython:
>>> Def fname (self, name ):
>>> Self. name = name
Look at the first line. The syntax is followed by the class name. Don't forget the "colon" to define a class.
Tip: the class name, first letter, has a non-text rule, preferably in upper case, so you need to identify and distinguish each class in the code.
The second line starts with a class method. As you can see, it is very similar to a function, but unlike a common function, it has a "self" parameter, it is used to reference the object itself.