Cat class ():
"" "class" ""
Tag = 'I was the cat'
DEF __init __ (Self, name, Age):
self.name = name
# private variable
Self .__ Age = Age
DEF set_age (Self, Age) :
Self .__ Age = Age
# return Self .__ Age
DEF show_info (Self):
REST = 'my name: {0}, {1} years old this year' .format (self.name, Self .__ Age)
Print (REST)
return REST
eAT DEF (Self):
"" "eat" ""
Print ( "cats like to eat fish")
DEF the catch (Self):
"" "and mouse" ""
Print ( "I can catch the mouse")
IF __name__ == ' __main__ ':
# Instantiated
cat_black = Cat ( 'black', 2)
cat_black.eat ()
cat_black.show_info ()
Print ( '--------')
# Print (cat_black.name) # black
# Print (cat_black.age)
# private variable can not access
# Print (cat_black .__ Age)
cat_black.name = 'white'
cat_black.show_info () # my name: white, 2 years old
Print ( '--------')
# set the age
# cat_black.set_age (7)
# cat_black.show_info () I call: white, 7 years old
Print (Cat.tag)
Print (cat_black.tag)
Print ( '--------')
# determine whether an instance of the class
print (isinstance (cat_black, Cat) ) # True
python Class (1)
Guess you like
Origin www.cnblogs.com/ericblog1992/p/11286921.html
Recommended
Ranking