Object oriented python - Recognition of classes and instances

'' 'Data attributes 1. 2. function attributes' '' 

# Create a class
class Chinese:
"This is a Chinese class,"

# class attributes
Money = 4000
# attention point classes and objects are used to access your property
def __ the __init (Self, name, Age, grender):
self.mingzi name =
self.nianji = Age
self.xingbie = grender


DEF TU ():
Print ( "spitting")

DEF cha_dui (Self):
Print ( "% S to jump the queue in front of "% self.mingzi)

DEF eat_food (Self, Food):
Print ("% S eating S% '% (self.mingzi, Food))

# instance attribute
p1 = Chinese ( "Yuan Hao", 18, "Boy")

Print (P1 .__ dict __) # object data attributes - attribute data instance only

Print (p1.mingzi)
Print (P1 .__ dict __ [ 'xingbie'])



Chinese.cha_dui(p1)
print(Chinese.__dict__)

= p1.team "Zhongguo"
Print (p1.team)


Print (the dir (P1))
p1.cha_dui ()
Print (p1.eat_food ( "Rice"))
Print (p1.money)

# class instance attributes must be able to access, examples of property class attributes not visit

# Print (Chinese)
# Print (Chinese.money)
# Chinese.tu ()
# Chinese.cha_dui ( 'Yuanhao')






property class # view
#Print (the dir (Chinese))

# View class the property dictionary
# Print (Chinese .__ dict __ [ 'Money'])
# Chinese .__ dict __ [ 'cha_dui'] ( 'yuanhao')

# Print (Chinese .__ name__)
# Print (Chinese .__ doc__ displays a)
# Print (Chinese .__ bases__)
Print # (Chinese class__ is .__)

# instantiate the object
# Chinese P1 = ()
#
# Print (P1)



# DEF Test ():
# pass
#
#
# print(test)


# Deletions class attribute change check 
class
Chinese: # class attribute Country = " China " DEF the __init__ (Self, name): the self.name = name DEF play_basketball (Self, Ball): Print ( " % S S is playing% " % (self.name, Ball)) # View Print (Chinese.country) # modify Chinese.country = " Japan " Print (Chinese.country) p1 = Chinese ( " alex " ) Print (p1.__dict__ ) Print (p1.country) # increased Chinese.resname = " XXXX " Print (Chinese.resname) Print (p1.resname) # attribute removing classes del Chinese.resname Print (Chinese. __dict__ ) # add a property function of class DEF eat_food (Self, Food): Print ( " % S% S XXXXX " % (the self.name, Food)) Chinese.eat = eat_food Print (Chinese. the __dict__ ) p1.eat ( " meat " ) def test(self): print("test") Chinese.play_ball=test #Chinese.play_ball("sss") p1.play_ball()


# Deletions instance attribute change check 
class Chinese: 
    # class attribute 
    Country = "China" 

    DEF the __init __ (Self, name): 
        the self.name = name 

    DEF play_basketball (Self, Ball): 
        Print ( "% S is playing% s"% (self.name, Ball)) 



p1 = Chinese ( "alex") 


# View 
Print (p1 .__ dict__) 
Print (p1.name) 
p1.play_basketball ( "basketball") 

# Add data attributes 
p1.age = 18 
Print (p1 dict__ magic .__) 
Print (p1.age) 


# Do not modify the properties of the underlying dictionary 
# P1 .__ dict __ [ 'Sex'] = "Maile" 
# Print (P1 .__ dict__ magic) 


# modify 
p1.age =. 19 
Print (P1 .__ dict__ magic) 

# delete 
del p1.age 
Print (p1 .__ dict__) 


# define a class,When a scope to use only Only when a scope to go with the 
class Mydate:
    Pass 
X = 10
y=20
Mydate.x=2
Mydate.y=8
print(x,y)
print(Mydate.x,Mydate.y)

  





Guess you like

Origin www.cnblogs.com/tangcode/p/11268926.html