from __future__ import unicode_literals
c = 20
_m = 190
class Animal(object):
__a = 11
def __init__(self):
self.__a
self.__run()
self._eat()
self.walk()
def __run(self):
print("Anlmal can run...")
def _eat(self):
print("Animal can eat...")
def walk(self):
print("Animal can walk...")
print(self.__a)
class Cat(Animal):
__a = 20
def __run(self):
print("Cat can run...")
def _eat(self):
print("Cat can eat...")
def walk(self):
print("Cat can walk...")
print(self.__a)
c = Cat()
Anlmal can run...
Cat can eat...
Cat can walk...
20
其实可以通过看,类中包含哪些属性,就知道那些方法被改写了。
print(dir(Animal()))
print(dir(Cat()))
['_Animal__a', '_Animal__run', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_eat', 'walk']
['_Animal__a', '_Animal__run', '_Cat__a', '_Cat__run', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_eat', 'walk']