Python first knows Python

Source: Internet
Author: User

"Class 1: Class"
#!/usr/bin/python#-*-coding:utf-8-*-classDog ():#construct    #class variables can not be explicitly defined in the head of the class, but it is best not to do so    #class variables do not explicitly differentiate between public and private, implicit rules: variable prefixes are __, which means that the variable is a private variable, otherwise public    def __init__(self, Name, age): Self.name=name Self.age= Agedefsit (self):Print(Self.name.title () +"is now sitting")    defRoll_over (self):Print(Self.name.title () +"rolled Over") Dog= Dog ("Dabai", 10) Dog.sit () Dog.roll_over ();PrintDog.name
"Class 2: Inheritance"
#!/usr/bin/python#-*-coding:utf-8-*-classAnimal ():def __init__(self, Animal_type, name): Self.animal_type=Animal_type; Self.name=name;classDog (Animal):def __init__(self, Name, age): Self.name=name#The syntax of Python2        #Python3 Syntax: Super (). __init__ (param1, param2, ...)Animal.__init__(Self,"Reptiles", name) self.age=Age Self.animal_type="Reptiles"        defsit (self):Print(Self.name.title () +"is now sitting")    defRoll_over (self):Print(Self.name.title () +"rolled Over") Dog= Dog ("Dabai", 10) Dog.sit () Dog.roll_over ();PrintDog.namePrintDog.animal_type

Python first knows 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.