Python classes (2)

A, classes and instances

Let the students write a class that stores information about the student, there is a method to integrate student information:

student.py

class Student (): 
    
    DEF  __init__ (Self, name, Age):
         "" " initialize student information " "" 
        self.name = name 
        self.age = Age 
        
    DEF get_desc (Self):
         "" " to get the description of the students '" " 
        long_name = STR (self.age) + '  ' + the self.name
         return long_name 

STU1 = Student ( ' Joker ' , 18 is )
 Print (stu1.get_desc ())

console:

Explain: __ init __ () to initialize attributes for instance, in get_desc () using the user access self.name and self.age attributes in this example, the basic information used to print the student.

Second, the default value assigned to property and modify property values

Each class attribute must have an initial value, even if it is 0 or an empty string, it must have. Under normal circumstances we use __init __ () is used to initialize, but if there is property

In __init __ () it has been assigned to the initial value, when the object is instantiated, without comprising providing an initial value for its parameters.

student.py

. 1  class Student ():
 2      
. 3      DEF  the __init__ (Self, name):
 . 4          "" " Initialization student information " "" 
. 5          the self.name = name
 . 6          self.age = 0
 . 7          
. 8      DEF get_desc (Self):
 . 9          "" " to give the student the description of "" " 
10          long_name = STR (self.age) + '  ' + the self.name
 . 11          return long_name
 12 is  
13 is STU1 student = ( ' Joker ' )
 14  Print(stu1.get_desc())
15 stu1.age = 18
16 print(stu1.get_desc())

console:

 

Explanation:

  Now, in Python calling __init __ () time to create a new instance, only you need to pass the name to instantiate an object, because the sixth line of code to the age given initial value, and the init method also requires only one addition to self parameter name, age, so the first printing is 0, look at the first 15 lines of code, this way we reassigned to age 18, the age of the printing of the second was 18, so if we need to modify the object the property can be used this way, but not the best way.

  Then use the second approach: to property assignment method:

student.py

. 1  class Student ():
 2      
. 3      DEF  the __init__ (Self, name):
 . 4          "" " Initialization student information " "" 
. 5          the self.name = name
 . 6          self.age = 0
 . 7          
. 8      DEF get_desc (Self):
 . 9          "" " to give the student the description of "" " 
10          long_name = STR (self.age) + '  ' + the self.name
 . 11          return long_name
 12 is          
13 is      DEF update_age (Self, new_age):
 14          " ""Age is used to update the students ' "" 
15         self.age = new_age
16 
17 stu1 = Student('joker')
18 print(stu1.get_desc())
19 stu1.update_age(19)
20 print(stu1.get_desc())

console:

Explanation:

  In our class, a new method for updating students of all ages, need to pass a new age, the age of the new method will be re-assigned to the age of age to complete the update. In the first 19 lines of code we pass 19, and then print out the information did show that we passed in age, as a way to update property values ​​are feasible. In fact, this is the way we used, under normal circumstances, we will not directly access the class attribute, so need to use the method to do "middleman", because of this, we can do some additional operations in the method, for example, in before re-assignment to determine whether the age of the illegal (negative), if not illegal to change the original property value or set of values ​​to the original default value.

 

 

  Friendship is so sweet and firm loyalty and unfailing divine feelings, as long as they do not lend money to you

 

Guess you like

Origin www.cnblogs.com/tizer/p/11013235.html