Classes for Python

Source: Internet
Author: User

Look at the Django source code, class inheritance do not understand, so learn to learn first.

1 #-*-coding:utf-8-*-2 3 classSchoolmember:4     def __init__(self,name,age):5Self.name =name6Self.age = Age7         Print 'init schoolmember:', Self.name, Self.age8         9     defTell (self):Ten         Print 'name:%s; age:%s'%(Self.name, Self.age) One          A          - classTeacher (schoolmember): -     def __init__(self,name,age,salary): the         #inherit the Schoolmember class, name, Age property???? Is this a succession or a rewrite?  -         #first, the parent class is inherited, first initialized in the parent class, and then inherited . -Schoolmember.__init__(self,name,age) -Self.salary =Salary +         Print 'init Teacher:', Self.name -          +     defTell (self): ASchoolmember.tell (self)#calling a method of the parent class at         Print 'Salary:', Self.salary -          -          - classStudent (schoolmember): -     def __init__(self,name,age,marks): -         #and inherit that . inSchoolmember.__init__(self,name,age) -Self.marks =Marks to         Print 'init Student:', Self.name +          -     defTell (self): the Schoolmember.tell (self) *         Print 'Marks:', Self.marks $         Panax Notoginseng          - if __name__=="__main__": thet = Teacher ('Yanggang', 20, 1000) +s = Student ('liming', 12, 86)  A #Members = [T,s] the #Print + #For member in Members : - #Member.tell ()

(1) After inheriting the parent class, using the instance of the subclass, the parent class will go first,

Schoolmember. __init__  

Summarize

Classes for Python

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.