Python Object-oriented Inheritance, python object-oriented

Source: Internet
Author: User

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()




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.