如何在类中使用方法来比较该类的两个对象

时间:2019-04-21 06:26:20

标签: python python-3.x oop

大家好,我是python中的oop的新手。我正在尝试编写一类学生,以获取学生的数量,年龄,身高和体重,并将这些信息存储为3个单独的列表。

我的目标是计算年龄,体重和身高的平均值。到目前为止,我没有任何问题。

在下一步中,我想比较该类的两个实例的平均年龄加上权重的平均值。

因为这是oop的练习,所以我应该使用该类的方法来执行。 但是,我不知道是否可以使用原始类(School类)中的方法来做到这一点,或者我应该创建一个子类来比较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))  

2 个答案:

答案 0 :(得分:0)

您混合使用类属性和实例属性。类属性在实例之间共享

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实例与一个实例(other):

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)

输出(格式之间用换行符表示):

Gimme ages, space seperated: 4 5 6
Gimme hights, space seperated: 22 44 66 88 99 
Gimme weights, space seperated:  20.2 20.2 20.2 20.2 20.2 20.2

Gimme ages, space seperated: 100
Gimme hights, space seperated: 100 
Gimme weights, space seperated: 100

5.0
44.0
20.2

100.0
100.0
100.0

Our pupils are younger:  True
Our pupils are smaller:  True
Our pupils are lighter:  True

更多信息:

答案 1 :(得分:0)

您可以通过创建一个具有更多参数的实例函数或使用带有staticmethod装饰器的静态方法样式来实现此目的

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的答案,您应该将变量声明为实例变量,以使它们在每个实例中的存储方式不同