|
class Particle (object):
def __init__ (self, name='', position=(0.0,0.0,0.0),velocity=(0.0,0.0,0.0),
spin=0.0):
self.position=position
self.velocity=velocity
self.name=name
self.spin=spin
def __str__ (self):
print('in particle str')
pos_str='({}:{}:{})'.format(self.position[0],self.position[1],self.position[2])
vel_str='({}:{}:{})'.format(self.velocity[0],self.velocity[1],self.velocity[2])
result_str='{} at {} with velocity {} and spin {}'.format(self.name,pos_str,vel_str,self.spin)
return result_str
class ChargedParticle (MassParticle):
def __init__ (self, name='', position=(0.0,0.0,0.0),velocity=(0.0,0.0,0.0),
spin=0.0, mass=0.0, charge=0.0):
MassParticle.__init__(self, name, position, velocity, spin, mass) #bad code
self.charge = charge
def __str__(self):
print('in charged str')
result_str = MassParticle.__str__(self)
result_str = result_str + ' and charge {}'.format(self.charge)
return result_str
photon = Particle(name='photon', spin=1.0)
print(photon)
tau = ChargedParticle(name='tau', spin=0.5, charge=-1.0, mass=1.77)
print(tau)
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-10-20 04:42
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社