First-Class Python object-oriented, parent and subclass (inheritance), examples of detailed __python

Source: Internet
Author: User

Concise python in the way, you understand the annotation: for reference


Class Schoolmember: #父类 def __init__ (self,name,age): #对象建立时马上对此对象初始化
Self.name=name
Self.age=age
Print (' Initialized schoolmember is%s '% self.name) #注意格式
def tell (self): #又一个方法
Print (' Name: '%s ' age:%s '% (self.name,self.age)) #格式
Class Teacher (Schoolmember): #子类引用
def __init__ (self,name,age,salary): #自己的方法添加额外元素
schoolmember.__init__ (Self,name,age) #此方法里要加入父类方法的引用, a critical step
self.salary=salary# Add specific content
Print (' Initialized teacher is%s '% self.name)
def tell (self):
Schoolmember.tell (self) #同上很关键
Print ("salary:%d"% self.salary)
Teacher1=teacher ("Li", 23,6000) #调用与验证

Teacher1.tell ()


#结果如下:
Initialized Schoolmember is Li
Initialized teacher is Li
Name: "Li" age:23
salary:6000

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.