"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