Python "Learning path 03" Object oriented

Source: Internet
Author: User

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# @Time: 2017/11/21 18:48
# @Author: mixiu26

class Role (object):
n=123 # class variables
def __init__ (self,name,role,weapon,life_value = 100,money = 15000):
# Execute __init () __ method before class execution
# __init__ ()----->> Data initialization: For data initialization assignment self--->> is equivalent to this in java. this.name = name means, who calls constructs, self is When so who
#----->> Constructors
#----->> Complete the initialization of the data when the object is created.
self.name = name #---->> member variable---->> static property
self.role = Role
Self.weapon = Weapon
# self.life_value = Life_value
self.__life_value = life_value # Sets the member variable to a private property, provides public access methods externally, and double-underlines before the variable
Self.money = Money

def shot (self): #-----Methods of >> classes---->> Dynamic Properties
print ("shotting .....")

def __got_shot (self): # member method Private, public access method function available externally ()
# In this class, you can modify the value of a private member property
Self.__life_value-=
print ("%s was hit ......"%self.name)

def buy_gun (self,gun_name):
print ("%s just bought%s"% (Self.name, gun_name))
# Public access methods available externally
def Show (self):
Print ("%s life left only:%s"% (Self.name,self.__life_value))

def function (self):
self.__got_shot ()

# destructors---->> are executed when the instance is disposed, ready for destruction, usually for some finishing touches, closing the memory space, closing the database connection, closing open temporary files
# Format: def __del__ (self):
# def __del__ (self): # automatically executes when the instance is freed and does not receive any parameters:
# print ("%s instance freed:"% self.name)

r1 = Role (' mixiu26 ', ' police ', ' AK46 ') # Create roles---->> Instantiate---->> Initialize classes---->> Create objects
# # Instantiation:---->> The process of turning a class into a concrete object, called instantiation
r2 = role (' hzh31 ', ' terrorlist ', ' B22 ') #---->> instance variables, scope is an instance of the instance itself--->>role
# r1.buy_gun (' AK46 ')
# r2.buy_gun (' B22 ')
# R1.got_shot () Attributeerror: ' Role ' object has no attribute ' Got_shot '
# R2.got_shot () Attributeerror: ' Role ' object has no attribute ' Got_shot '
r1.function ()
r2.function ()
r1.show ()
r2.show ()

# Print (r1.self.__life_value) attributeerror: ' Role ' object have no attribute ' self '

#---->> actually equivalent to the application of space in the stack, in fact, the equivalent in the __iniy () __ Application of space R2, and then role (), is actually quite with the heap in the memory opened a space
#---->> role is equivalent to initializing the data, name = NULL, role = Null,weapon = NULL, there is a method area initialization, and then the method area in role has a memory address
#---->> role () gives the address of the method area in role to role () and then creates an association between the two, so the object generated here will be able to find the member method in the role by this address value .
#---->> After this, the role () method is initialized, and the role () method itself obtains an address value, and then the address value is given to R1,R1, which points to role ()
#---->> What about self? In fact, role () himself is self, the whole role () object is this, so the object of this visit is the R1, then it is the equivalent of R1
#---->> R1.buy_gun (), you will find the method area of the Buy_gun () method, there is no error, and then load the method into the stack memory, and we are now through R1 to call, so
#---->> now self represents R1, so in the __inin () __ Method self.name = name---->> is equivalent to R1.name = name, so it's equivalent to
# the ' mixiu26 ' in---->> R1 gave name to the role of police

# Print (r1.n,r1.name) # 123 mixiu26
# Print (r2.n,r2.name) # 123 hzh31
#
# # Modify variable values:
# r1.name = "Neinei"
# r2.name = "Paofu"
#
# Print (r1.n,r1.name) # 123 Neinei
# Print (r2.n,r2.name) # 123 Paofu

# Add a property value for R1:
# r1.dream = "Good Girl"
# Print (r1.n,r1.name,r1.dream) # 123 Neinei Good girl = = >> Actually there is an implied (R1,r1.n,r1.name,r1.dream)
#
# # Delete member property values:
# del R1.dream
# # print (r1.n,r1.name,r1.dream) # attributeerror: ' Role ' object has no attribute ' dream '
#
# # Modify the value of the class variable:
# R1.N = "789"
# Print (r1.n,r1.name) # 789 Neinei Note, this is said to modify the value of the class variable, in fact, the n= xxx copied to the instance variable, and then the instance variable to modify N, in fact, this N and class variable n has
# # is not the same n, so we can modify n is modified in the instance variable, and the member variable n is not related, so see the result is, R1 print the modified value, r2 or member variable value
# Print (r2.n,r2.name) # 123 Paofu

# Modify member variable N: Note that the read of the variable follows the nearest principle, in the previous procedure, we call the instance variable r1 modify the member variable N, we know that his principle is to put n=xxx, copied a copy to the instance variable R1
# So when role takes the value of modifying N, R1 is not affected because it reads N in its own instance variable, and the N of your member variable has no effect on my instance variable, and there is no such instance variable in R2
# So the read is also the n in the member variable, so when the value of the member variable changes, R2.N also changed

# ROLE.N = "ABC"
# Print (r1.n,r1.name) # 789 Neinei
# # del R1 # Neinei instance release: When a variable disappears, it automatically executes the destructor to reclaim the memory space, the object does not disappear, and the method is executed when all memory space is reclaimed when the program execution finishes exiting
# Print (r2.n,r2.name) # ABC Paofu

# class Variables--->> public properties, saving memory space
# Constructor--->> complete data initialization when creating an object:



Python "Learning path 03" Object oriented

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.