Inheritance in Python

Source: Internet
Author: User

Inheritance: Is the relationship between classes and classes in Python, mainly using classes that have already been written to produce new classes, and subclasses inherit properties or methods from the parent class!

1 #-*-coding:utf-8-*-2 classPerson :3     def __init__(self,race,age,height,sex):4Self.race =Race5Self.age = Age6Self.height =Height7Self.sex =Sex8     defTalk (self,language):9Self.language =languageTen         ifLanguage = ='中文版': One             Print 'I am seapking%s'%Self.language A         Else: -             Print "I don ' t konw i ' m speaking what language now!" -  the  - classPersondemo (person):#Inherit parent class -     def __init__(self,name,country,like,race,age,height,sex): -Person.__init__(Self,race,age,height,sex)#inheriting parent class properties +Self.name =name -Self.country =Country +Self.likes = like A  at     definfos (self): -         Print " "This is my informaion: - Name:%s - Age:%s - Skin Tone:%s - Height:%s in Gender:%s - Nationality:%s to Hobbies:%s +               " "%(Self.name,self.age,self.race,self.height,self.sex,self.country,self.likes) -  the #instantiating an object *p = Persondemo ('Dante','CN',' Games','Yellow', 27,177,'male') $ #methods that call the subclass itselfPanax Notoginseng P.infos () - #invoking a method inherited from a parent class theP.talk ('中文版')

Results obtained:

 is my informaion:                name: Dante                                 interracial: Yellow                177                 Sex: Male                Nationality: CN                Hobby: Games              I am seapking 中文版

Writing a subclass only requires it to specify its parent class, and the rest is basically similar to the normal class

Inheritance in Python

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.