关于Python 面向对象寻值的问题. How the number be found in the OOP in Python
今天在看Python面向对象的时候看到了一个很有意思的问题
Today. When i learning the OOP in python , I found a very interesting Question that how a number be found in the parent class and the child class .
First of all, let us define a class Animals
class Animals: age = 0
Then let us define a child class
class Dog(Animals): "Dog class"
next, we just create a main method :
zhang_san= Dog() li_si = Dog() zhang_san.age = 1 Dog.age = 10 print(zhang_san.age) print(li_si.age)
We will get a result that the age in zhang_san is absolutely 1 .But how about the age in object li_si ?
Actually the result of li_si is 10.
So why ?
if we also set the value of age of li_si as any numbers such as 99
The outcome will be different.
So why ?
As the number of age is a class number , belong to the class of Animals and the dog is the child node of Animals
When you want to find a number in python . the program will to find the child node in first , if can not find any number you want the python will countine to search according to their parent nodes. At the end of the process , if it is still could not find a number , the program will throw error .