add update_position as a class method #9
1 changed files with 6 additions and 5 deletions
|
@ -52,17 +52,18 @@
|
|||
],
|
||||
"source": [
|
||||
"class Particle:\n",
|
||||
" def __init__(self, mass, velocity):\n",
|
||||
" def __init__(self, mass, velocity, position):\n",
|
||||
" self.mass = mass\n",
|
||||
" self.velocity = velocity\n",
|
||||
" self.position = position\n",
|
||||
"\n",
|
||||
" def momentum(self):\n",
|
||||
" return self.mass * self.velocity\n",
|
||||
"\n",
|
||||
" def update_position(self, position, dt):\n",
|
||||
" return position + self.velocity * dt\n",
|
||||
" def update_position(self, dt):\n",
|
||||
" return self.position + self.velocity * dt\n",
|
||||
"\n",
|
||||
"particle = Particle(mass=2.1, velocity=0.8)\n",
|
||||
"particle = Particle(mass=2.1, velocity=0.8, position = 8.2)\n",
|
||||
"print(particle.momentum())"
|
||||
]
|
||||
},
|
||||
|
@ -80,7 +81,7 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"position = 8.2\n",
|
||||
"\n",
|
||||
"new_position = particle.update_position(position, dt=0.1)\n",
|
||||
"print(new_position)"
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue