1.1
class Animal: def __init__(self,name): self._name = name def shout(self): print('{} shouts'.format(self.__class__.__name__)) @property def name(self): return self._name class Cat(Animal): pass cat = Cat('garfield') cat.shout() cat.name #Through inheritance, the cat class directly inherits the properties and methods of the parent class without writing code
1.2
class Animal: __COUNT = 100 HEIGHT = 0 def __init__(self,age,weight,height): self.__COUNT += 1 self.age = age self.__weight = weight self.HEIGHT = height def eat(self): print('{} eat'.format(self.__class__.__name__)) def __getweight(self): print(self.__weight) @classmethod def showcount1(cls): print(cls.__COUNT) @classmethod def __showcount2(cls): print(cls.__COUNT) def showcount3(self): print(self.__COUNT) class Cat(Animal): NAME = 'CAT' __COUNT = 200 c = Cat(3,5,15) c._Animal__getweight() Cat.__dict__
1.3
class Animal: def shout(self): print('Animal shouts') class Cat(Animal): def shout(self): print('miao') def shout(self): # print(super()) # print(super(Cat,self)) super().shout() super(Cat,self).shout() self.__class__.__base__.shout(self) c = Cat() c.shout()
class Animal: def shout(self): print('Animal shouts') class Cat(Animal): def shout(self): print('miao') def shout(self): # print(super()) # print(super(Cat,self)) super().shout() super(Cat,self).shout() self.__class__.__base__.shout(self) c = Cat() c.shout()
1.4
class Animal: @classmethod def class_method(cls): print('class_method_animal') @staticmethod def static_method(): print('static_method_animal') class Cat(Animal): @classmethod def class_method(cls): print('class_method_cat') @staticmethod def static_method(): print('static_method_cat') c = Cat() c.class_method() c.static_method() #These methods can be overridden, the principle is the same, the search order of the attribute dictionary
1.5
class A: def __init__(self,a): self.a = a class B(A): def __init__(self,b,c): A.__init__(self,b+c) self.b = b self.c = c def printv(self): print (self.b) print (self.a) #Good habit , if init is defined in the parent class, you can call it in init in the subclass instead f = B(200,300 ) f.printv() f.__dict__ f.__class__.__bases__
1.6
class Animal: def __init__(self,age): print('animal init') self.__age = age def show(self): print(self.__age) class Cat(Animal): def __init__(self,age,weight): super().__init__(age) print('cat init') self.__age = age +1 self.__weight = weight c = Cat(10,5) a = Animal(100) c. __dict__ # {'_Animal__age': 10, '_Cat__age': 11, '_Cat__weight': 5} a. __dict__ # {'_Animal__age': 100} c.show() # 10 # Own private attributes, you should be yourself The method of modifying, and reading, do not rely on other classes and methods
1.7
class Animal: def __init__(self,age): print('animal init') self.age = age def show(self): print(self.age) class Cat(Animal): def __init__(self,age,weight): # super().__init__(age) print('cat init') self.age = age +1 self.weight = weight c = Cat(10,5) a = Animal(100) c.__dict__ #{'age': 11, 'weight': 5} a.__dict__ #'age': 100}