Create a python classes and objects

Recently come into contact with some object-oriented programming content, involving a number of objects and classes only, here's review, accompanied by learning the code below

. 1  class Person:
 2      Hair = ' Black '       # defined herein a class variable Hair 
. 3      DEF  the __init__ (Self, name = ' Charlie ' , Age = 22 is ):
 . 4          # here add two instance variables of the Person object 
. 5          Self. = name name
 . 6          self.age = Age
 . 7  
. 8      # D say a defined method 
. 9      DEF say (Self, Content):
 10          return Content
 . 11  
12 is  # constructor call the person class, and returns an object of the Person object is assigned to P 
13 is P =The Person ()
 14  # outputs p name and age two instance variables, then the two default values to variables or 
15  Print (p.name, ' \ n- ' , p.age)
 16  # access name instance variables p, and re-assignment for his 
17 p.name = ' wind_under_the_wing ' 
18  
19  # calling say method of p, 
20  
21  Print ( ' the Hello, the I AM ' + p.name + ' , ' + p.say ( ' is available for purchase to My Blog ' ))

__init__ is the constructor for creating an object, a class must have at least one construction methods, call the constructor of the class to create objects of this class

 

 

Guess you like

Origin www.cnblogs.com/wind-under-the-wing/p/11961328.html