013:class and objects > Simple inheritance

Source: Internet
Author: User

1. All classes are inherited directly or indirectly with object.

2. The super () method can be used to access the methods of the parent class, and if the subclass has a method with the same name as the parent class, the subclass overrides the method of the parent class.

3. After the definition of a class-level member, all descendants share a class-level member

classContact (object):#class-Level member variablesContact_list = []    def __init__(self, name, email, telephone):#member variables at the object levelSelf.name =name Self.email=Email Self.telephone=telephone Contact.contact_list.append (self) contact_a= Contact ('Tom','[email protected]', 123456) Contact_b= Contact ('Lucy','[email protected]', 123457)Print('Contact :') forCinchcontact.contact_list:Print(c.name)classFriend (Contact): Friend_list= []    #overriding methods of the parent class    def __init__(self, name, e-mail, telephone, age):#calling a method of the parent classSuper (Friend, self).__init__(name, email, telephone) self.age=Age Friend.friend_list.append (self) friend_a= Friend ('Jack','[email protected]', 123458, 22) Friend_b= Friend ('Jim','[email protected]', 123459, 23)    #It's a little strange that the child class has quite the same contact_listPrint(contact.contact_list)Print(friend.contact_list)Print('Friends:') forFinchfriend.friend_list:Print(F.name, F.age)

013:class and objects > Simple inheritance

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.