Examples of methods
Examples of methods and instance variables are the same as an instance (or object) characteristic of individuals. Examples of the method described below.
Method in class in function definition . Examples of the method defined while its first argument should be self, this process is the current instance and bind to this method, the method becomes instance method.
class Animal(object):
"""定义动物类"""
def __init__(self, age, sex = 1, weight = 0.0):
self.age = age
self.sex = sex
self.weight = weight
def eat(self):
self.weight += 0.05
print('eat...')
def run(self):
self.weight -= 0.01
print('run...')
a1 = Animal(2, 0, 10.0)
print('a1 体重:{0:0.2f}'.format(a1.weight))
a1.eat()
print('a1 体重:{0:0.2f}'.format(a1.weight))
a1.run()
print('a1 体重:{0:0.2f}'.format(a1.weight))
The results are as follows:
| a1 Weight: 10.00
EAT ...
a1 Weight: 10.05
RUN ...
a1 Weight: 10.04 |
05 RUN ... a1 Weight: 10.04 |
---|