Python 如何在类中使用方法来比较该类的两个对象
大家好,我是python中oop的新手。我试着写一个学生班,得到学生人数、年龄、身高和体重,并将这些信息存储为3个单独的列表 我的目标是计算平均年龄、体重和身高。到目前为止我没有任何问题 在下一步中,我想比较两个类实例的平均年龄加上权重的平均值 因为这是oop中的一个练习,所以我应该用类的方法来做。 但是,我不知道是否可以在原始类(class-School)中使用方法,或者我应该创建一个子类来比较School类的两个实例的属性 非常感谢您的帮助 这是我的密码:Python 如何在类中使用方法来比较该类的两个对象,python,python-3.x,oop,Python,Python 3.x,Oop,大家好,我是python中oop的新手。我试着写一个学生班,得到学生人数、年龄、身高和体重,并将这些信息存储为3个单独的列表 我的目标是计算平均年龄、体重和身高。到目前为止我没有任何问题 在下一步中,我想比较两个类实例的平均年龄加上权重的平均值 因为这是oop中的一个练习,所以我应该用类的方法来做。 但是,我不知道是否可以在原始类(class-School)中使用方法,或者我应该创建一个子类来比较School类的两个实例的属性 非常感谢您的帮助 这是我的密码: class School:
class School:
avg_age = 0
avg_heigt = 0
avg_weight = 0
def __init__(self):
self.n =int(input())
self.list_ages = [float(x) for x in input().split(" ")]
self.list_higt = [float(x) for x in input().split(" ")]
self.list_weight = [float(x) for x in input().split(" ")]
def get_av(self):
School.avg_age = sum(self.list_ages) / len(self.list_ages)
School.avg_heigt = sum(self.list_higt)/len(self.list_higt)
Scoohl.avg_weight = sum(self.list_weight)/len(self.list_weight)
return("{},{},{}".format(School.avg_age,School.avg_heigt,School.avg_weight))
可以混合使用类属性和实例属性。类属性在实例之间共享:
class Banks:
total = 0
def __init__(self,name,money):
self.name=name
self.money=money
Banks.total += money
b1 = Banks("one",100)
b2 = Banks("two",5000000)
# prints 5000100 - all the money of all banks,
# does not help you comparing avgs ov instances at all
print(b1.total)
输出:
5000100
每个实例需要单独的平均值,并且需要一个函数将一个实例(self
)与另一个实例进行比较:
class School:
def __init__(self):
# self.n =int(input()) # not needed - use len(one of your lists)
list_ages = [float(x) for x in input("Gimme ages, space seperated: ").split()]
list_hight = [float(x) for x in input("Gimme hights, space seperated: ").split()]
list_weight = [float(x) for x in input("Gimme weights, space seperated: ").split()]
# shortest list downsizes all other lists
self.list_ages, self.list_hight, self.list_weight = zip(
*zip( list_ages,list_hight,list_weight ))
def get_av(self):
self.avg_age = sum(self.list_ages) / len(self.list_ages)
print(self.avg_age)
self.avg_height = sum(self.list_hight) / len(self.list_hight)
print(self.avg_height)
self.avg_weight = sum(self.list_weight) / len(self.list_weight)
print(self.avg_weight)
return self.avg_age, self.avg_height, self.avg_weight
def compare(self,other):
self.get_av()
other.get_av()
print("Our pupils are younger: ", self.avg_age < other.avg_age)
print("Our pupils are smaller: ", self.avg_height < other.avg_height)
print("Our pupils are lighter: ", self.avg_weight < other.avg_weight)
c = School() # 4 5 6 22 44 66 88 99 20.2 20.2 20.2 20.2 20.2 20.2
d = School() # 100 100 100
c.compare(d)
更多信息:
您可以通过使用一个或多个参数创建一个实例函数,或使用decorator的静态方法样式来实现
class School:
# your code
def compare_to(self, other)
# compare an instancte with other instance
# your compare code here
pass
@staticmethod
def compare(first_school, second_school):
# first and second school are two objects of class school
# your compare code
pass
你可以用两种方式调用你的函数
s1 = School()
s2 = School()
# Your init data
s1.compare_to(s2)
School.compare(s1, s2)
编辑:作为@Patrick answer,您应该将变量声明为实例变量,以使每个实例的存储方式不同
s1 = School()
s2 = School()
# Your init data
s1.compare_to(s2)
School.compare(s1, s2)