在另一个 class PYTHON 中使用一个 class 对象
Using one class object in another class PYTHON
在此代码中,构造函数首先询问从另一个名为 Sun 的 class 创建的太阳的名称。 Sun 创建一个具有 4 个属性的太阳对象:名称、半径、质量和温度。我想在这个太阳系 class 中做的是计算所有行星的总质量加上太阳物体的质量,但我很困惑如何访问我通过太阳创建的太阳物体的属性class。还没真正找到好的解释
我的代码如下:
class SolarSystem:
def __init__(self, asun):
self.thesun = asun
self.planets = []
def addPlanet(self, aplanet):
self.planets.append(aplanet)
def showPlanet(self):
for aplanet in self.planets:
print(aplanet)
def numPlanets(self):
num = 0;
for aplanet in self.planets:
num = num + 1
planets = num + 1
print("There are %d in this solar system." % (planets))
def totalMass(self):
mass = 0
sun = self.thesun
sunMass = sun.mass
for aplanet in self.planets:
mass = mass + aplanet.mass
totalMass = mass + sunMass
print("The total mass of this solar system is %d" % (mass))
以下代码对我有用,我只需要将 totalMass()
中的 print
语句更改为使用 totalMass
,而不是 mass
:
class SolarSystem:
def __init__(self, asun):
self.thesun = asun
self.planets = []
def addPlanet(self, aplanet):
self.planets.append(aplanet)
def showPlanet(self):
for aplanet in self.planets:
print(aplanet)
def numPlanets(self):
num = 0;
for aplanet in self.planets:
num = num + 1
planets = num + 1
print("There are %d in this solar system." % (planets))
def totalMass(self):
mass = 0
sun = self.thesun
sunMass = sun.mass
for aplanet in self.planets:
mass = mass + aplanet.mass
totalMass = mass + sunMass
print("The total mass of this solar system is %d" % (totalMass))
class Sun:
def __init__(self, name, radius, mass, temp):
self.name = name
self.radius = radius
self.mass = mass
self.temp = temp
test_sun = Sun("test", 4, 100, 2)
test_solar_system = SolarSystem(test_sun)
test_solar_system.totalMass()
在此代码中,构造函数首先询问从另一个名为 Sun 的 class 创建的太阳的名称。 Sun 创建一个具有 4 个属性的太阳对象:名称、半径、质量和温度。我想在这个太阳系 class 中做的是计算所有行星的总质量加上太阳物体的质量,但我很困惑如何访问我通过太阳创建的太阳物体的属性class。还没真正找到好的解释
我的代码如下:
class SolarSystem:
def __init__(self, asun):
self.thesun = asun
self.planets = []
def addPlanet(self, aplanet):
self.planets.append(aplanet)
def showPlanet(self):
for aplanet in self.planets:
print(aplanet)
def numPlanets(self):
num = 0;
for aplanet in self.planets:
num = num + 1
planets = num + 1
print("There are %d in this solar system." % (planets))
def totalMass(self):
mass = 0
sun = self.thesun
sunMass = sun.mass
for aplanet in self.planets:
mass = mass + aplanet.mass
totalMass = mass + sunMass
print("The total mass of this solar system is %d" % (mass))
以下代码对我有用,我只需要将 totalMass()
中的 print
语句更改为使用 totalMass
,而不是 mass
:
class SolarSystem:
def __init__(self, asun):
self.thesun = asun
self.planets = []
def addPlanet(self, aplanet):
self.planets.append(aplanet)
def showPlanet(self):
for aplanet in self.planets:
print(aplanet)
def numPlanets(self):
num = 0;
for aplanet in self.planets:
num = num + 1
planets = num + 1
print("There are %d in this solar system." % (planets))
def totalMass(self):
mass = 0
sun = self.thesun
sunMass = sun.mass
for aplanet in self.planets:
mass = mass + aplanet.mass
totalMass = mass + sunMass
print("The total mass of this solar system is %d" % (totalMass))
class Sun:
def __init__(self, name, radius, mass, temp):
self.name = name
self.radius = radius
self.mass = mass
self.temp = temp
test_sun = Sun("test", 4, 100, 2)
test_solar_system = SolarSystem(test_sun)
test_solar_system.totalMass()