Python Learning-basic knowledge of classes

Source: Internet
Author: User

#!/usr/bin/env python#-*-coding:utf-8-*-#Author linux kuclassDog ():def __init__(self, name): Self.name=namedefBulk (self):Print("%s:wang Wang wang!"%self.name) Dog1= Dog ("Dog1") dog2= Dog ("dog2") Dog1.bulk () Dog2.bulk ()classRole (object):#class namen = 222#class Variable: First find the instance variable and then find the class variable    def __init__(Self, name, role, weapon, life_value=100, money=15000, secret ="Secret"):#constructor Function#The constructor, the object of this thing, I think is again in memory to open up a position, equivalent to giving a nameplate number. #do some initialization work for classes at instantiation time. #memory is run, the number of memory is actually at the time of assignment, such as R1 = Role (r1,xxx,xx), time to apply, and then run the constructor when the specific content of the input#when the constructor is run, self is the value that R1 passed in to R1, so self is actually used to differentiate. So, the argument self is reserved for the instantiated name. Self.name = Name#instance variable (static property), scoped to the instance itselfSelf.role =role Self.weapon=Weapon Self.life_value=Life_value Self.money=Money self .__secret= Secret#private properties, can only be changed in internal methods#The following function is in the class and is not stored in the instantiated place .    defShot (self):#the self here is the specific name of the instantiation, marking the address        Print("Shooting ...")    defGot_shot (self):#method of the class, function (dynamic property)        Print("Ah...,i got shot ...")    defShow_secret (self):Print("The secret is%s"%self.__secret)    def __secret_method(self):#private method, can only be called internally        Print(self.)__secret)    defShow_secret_method (self): self.__secret_method()    defBuy_gun (Self, gun_name):Print("just bought%s"%gun_name)defShow_all_vari (self):Print("All the vari:%s"%{self.name,self.role,self.money,self.life_value})#Destructors : Executed when an instance is disposed, destroyed, and usually used to do some finishing work, such as closing some database links, opening temporary files#This function will be used automatically when the program exits, or it can be manually advanced using the DEL R1    def __del__(self):Print("End the example.%s"%self.name)Print("or do the necessary work.") R1= Role ('Alex','Police','AK47')#generate a roleR1.show_all_vari () r1.got_shot ( )#the actual is Role.got_shot (R1)Print("This is secret method:") R1.show_secret_method ()Print("This was Secret property:") R1.show_secret ()Print("---------") R2= Role ('Jack','Terrorist','B2')#generate a roleR2.got_shot ()

Python Learning-basic knowledge of classes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.