# Demand: washing machine, function: laundry # 1, the definition of a washing machine class "" " class class name (): Code " "" class Washer (): DEF Wash (Self): Print ( " can do the laundry " ) # 2 create Object # object name = class name () haier = Washer () # 3, to verify the results of # print haier objects Print (haier) # use the wash function - an instance method / object method - object name .wash () haier.wash () # class: washing machine, function: to wash clothes class washer (): DEF Wash (Self): Print ( " laundry" ) Print (self) haier1 = Washer () Print (haier1) # <__ main __. Washer Object AT 0x00000223270D9710> # haier.wash () # Cleaning <__ main __. Washer Object AT 0x00000223270D9710> # since the print object and the print self obtained memory address is the same, so the self means that the function is called object haier2 = Washer () Print (haier2) # a class can create multiple objects; when multiple objects are called function, self address is not the same added and obtaining object attribute property that is characteristics, such as: the width, height, weight of the washing machine attributes of the objects may be added and acquired outside the class, also can be added and acquiring inside the class 1 outer class adds object property # grammatical object name attribute. name = value DG: haier.width = 500 class Washer (): DEFWash (Self): Print ( " laundry " ) haier1 = Washer () # add object properties haier1.width = 400 haier1.height = 500 # outside the class get the object attribute # get the name of the object attribute property name. Print ( " washing machine width:% S " % haier1.width) # width washing machine are: 400 Print ( " the height of the washing machine are:% S " % haier1.height) # height of the washing machine are: 500 Print ( " ------ -------------------------- " ) # class objects get inside the property # Syntax: self attribute name. Class myWasher (): DEF Wash (Self): Print ( " laundry " ) # get object properties DEF print_info (Self): # . Self attribute name # Print (self.width) Print ( " washing machine the width is:% S " % self.width) Print ( " the height of the washing machine are:% S " % self.height) my_haier = myWasher () # add the attribute my_haier.width = 400 my_haier.height = 500 # object calls a method my_haier.print_info () # cube method # in python, __ xx__ function called magic method, referring to the function with special features # experience __init__ # __init __ () method function: initialize the object # __init __ () method, the default when you create an object is called, does not need to manually call # __init __ Self parameters (self) is not required developers pass, python interpreter will automatically pass the current object references past # target: init magic method definitions set the initialization properties and access calls "" " 1 define a class: the init magic method: width and height add instance methods: instance properties 2 to create an object 3 to verify the results of calling the instance method " "" class Washer (): DEF __init__ (Self): # add examples of properties self.width = 500 self.height = 800 DEF print_info (Self): Print ( " width washing machine are:% S " % self.width) Print ( " the height of the washing machine are:% S " % self.height) Haier = Washer () haier.print_info () # Washer the width: height 500 washing machine are: 800 # __init __ () with parameters # ponder: a class can create multiple objects, how to set up different initialization properties of different objects? A: transmission parameter class Washer (): DEF the __init__ (Self, width, height): # Add instance attributes self.width = width self.height = height DEF print_info (Self): Print ( " width washing machine are:% S " % self.width) Print ( " the height of the washing machine are:% S " % self.height) Haier = Washer (100,200 ) haier.print_info () # the width of the washing machine are: the height of the washing machine 100: 200 is __str__ () when using the print output destination, the default print target memory address. If __str__ class defines a method, it will return from the printing process this data class Washer (): DEF the __init__ (Self): self.width = 300 DEF __str__ (Self): return ' explanation: Class Description Description of the state of the object or ' Haier = Washer () Print (Haier) # explanation: explanatory or state of the object class # __del __ () # when deleting objects, python interpreter is also called by default __del __ () method of class Washer (): DEF __init__ (Self): self.width = 300 DEF __del__ (Self): Print ( " Object has been deleted " ) Haier = Washer () # object has been deleted # del Haier
Object-oriented methods python
Guess you like
Origin www.cnblogs.com/spp666/p/12098412.html
Recommended
Ranking