Object-oriented concepts are classes (class) and instances (Instance), and it is important to keep in mind that classes are abstract templates, such as student classes, and instances are specific "objects" that are created from classes, each with the same method, but the data may be different for each object.
As an example of the student class, in Python, the definition class is defined by the class
keyword
Once you have defined the Student
class, you can create Student
Student
an instance from the class by using the class name + () to create the instance.
Because a class can act as a template, you can force a number of attributes that we think must be bound to be filled in when creating an instance. By defining a special __init__
method
If you want the internal properties to be inaccessible externally, you can add two underscores to the name of the property, and __
in Python, the variable name of the instance __
becomes a private variable (private), which can only be accessed internally and cannot be accessed externally.
It is important to note that in Python, the variable name is similar, which starts with a double underscore, and ends with a double underscore, which is a special variable that __xxx__
can be accessed directly, not a private variable, so __name__
__score__
the variable name cannot be used.
There are times when you see an instance variable name that starts with an underscore, such as an _name
instance variable that can be accessed externally, but, as you see in the rules, when you look at a variable like this, the meaning is, "although I can be accessed, please treat me as a private variable and don't feel free to access it."
is an instance variable that starts with a double underscore not necessarily externally accessible? Actually, it's not. cannot be accessed directly __name
because the Python interpreter __name
has changed the variable _Student__name
so that it can still _Student__name
access the __name
variable
In OOP programming, when we define a class, we can inherit from an existing class, and the new class is called a subclass (subclass), and the inherited class is called the base class, the parent class, or the superclass (base-Class, Super-Class).
For example, we have written a class named Animal
, and there is a run()
way to print directly
When we need to write Dog
and Cat
class, we can inherit directly from the Animal
class:
class Dog(Animal): passclass Cat(Animal): pass
When both the subclass and the parent class have the same run()
method, we say that the child class run()
overrides the parent class, and the subclass is run()
always called when the code is running run()
. In this way, we gain another benefit of inheritance: polymorphic
For a variable, we just need to know that it is a Animal
type, without knowing exactly what its subtype is, you can safely invoke run()
the method, and the method that is called is the function of the, run()
or the Animal
Dog
Cat
Tortoise
object, Determined by the exact type of the object at run time, this is the true power of polymorphism: The caller just calls, regardless of the details, and when we add a Animal
subclass, simply make sure that the run()
method is written correctly, regardless of how the original code is called. This is the famous "opening and shutting" principle.
Use Type ()
First, let's judge the object type and use the type()
function:
Basic types can be used to type()
judge the inheritance of class, it is type()
very inconvenient to use. We want to determine the class type, you can use the isinstance()
function
Always prefer to use isinstance () to determine the type
If you want to get all the properties and methods of an object, you can use a dir()
function that returns a list that contains a string, for example, to get all the properties and methods of a Str object
As can be seen from the above example, when writing a program, do not use the same name for instance properties and class properties, because instance properties of the same name will mask the class properties, but when you delete an instance property and then use the same name, the class attribute is accessed.
I tidy up the Python rules (2)