Day 8 python learning Notes

Class 1 three predefined function

  After 1.1 __repr __ (self), defines an instance of the class, the class name if the direct hit, this function will return the value of output

  After 1.2 __str __ (self), defines an instance of the class, if a print (the class name), the output of this function will return the value

  1.3 __del __ (self), destructor, to remove examples

When two kinds of variables define a class of 2

  2.1 Variables associated with the examples, such as the establishment of a class book, then the title, price, and examples of these are directly related, so when these variables are assigned to the initialization function, the need to use self. Variables with

  2.2 with no associated instance variables, such as wondering examples of a few books, so the definition of this variable can not use self. Variable manner, but should be in the initialization function previously defined variables, and do count in the initialization function, the use the class name. variables way

  NOTE: For treatment 2.2, not by calling the associated object instance of the object class name and call unrelated objects whose value is the same, but if the call is not associated object value of the object instance to modify, when the class name to call value does not change, while the value of an instance of an object becomes a call to the modified value

Class 3 inside the two functions

  With about 3.1 instance, when there is self-defined in the parameter list

  3.2 has nothing to do with the example, the definition of non-self in the parameter list

4 for the class methods can not be arbitrarily modified, it can be turned into property modifier is to add, delete, and so can not be assigned and the like. such as:

    the @Property 

    DEF Age (Self): 

        the Return 5 

    @ age.setter 

    Def Age (Self): 

        The raise AttributeError ( 'prohibit the assignment of age') 

    @ age.deleter 

    DEF Age (Self): 

        The raise AttributeError ( 'Age can not be deleted')

 

5 Inheritance

    5.1 The basic format

        

class name of the class (class-yl):

 

    5.2 in two ways inherited class initialization

       

Super (). the __init__ () 

base class. the __init__ ()

 

Guess you like

Origin www.cnblogs.com/zhuome/p/11330205.html