Python Object-oriented Inheritance, python object-oriented
There is nothing to say. Inheritance mainly refers to inheriting some methods of the parent class. The code is very detailed.
#! /Usr/bin/env python # coding: utf-8class Father (object): # New Class def _ init _ (self): self. name = 'liu' self. familyName = 'yan' def Lee (self): print 'I am the parent class function Lee 'def Allen (self): print "I am the parent class function Allen" class Son (Father): def _ init _ (self): # Father. _ init _ (self) # The classic class executes the parent class constructor super (Son, self ). _ init _ () # New Class executes the parent class constructor self. name = 'feng' def Aswill (self): # adds the print 'son function to the subclass. bar 'def Lee (self): # override the parent class function Lee print 'subclass overwrites the parent class function Lee 's1 = Son () print "inherits the parent class name" + s1.FamilyNameprint "overwrites the parent class name", s1.names1. lee () # The subclass overrides the parent class function Lees1.Allen () # The subclass inherits the parent class function Allen
The sequence of inheriting multiple classes. Classic class inheritance takes precedence over depth, and is a BUG. New classes take precedence over extensiveness. New classes should be used to define classes.
New category
Class A (object): # Write def _ init _ (self): print 'this is from a' def test (self ): print 'this is test from a' class B (A): def _ init _ (self): print "This is from B" class C (): def _ init _ (self): print "This is from C" def test (self): print "This is test from C" class D (B, C ): def _ init _ (self): print 'this is d' T1 = D () T1.test ()
Classic
class A(): def __init__(self): print 'This is from A' def test(self): print 'This is test from A'class B(A): def __init__(self): print "This is from B" class C(A): def __init__(self): print "This is from C" def test(self): print "This is test from C" class D(B,C): def __init__(self): print 'this is D' T1=D()T1.test()