1.设计一个Person,包含姓名、年龄和性别三个私有成员属性,另外Person类还包含males和females两个私有类属性,用来记录男生和女生的数量, 可以通过number_male和numb

class Person:
    __males = 0
    __females = 0

    def __init__(self,name,age,sex):
        self.__name = name
        self.__age = age
        self.__sex = sex

        if self.__sex == "男":
            Person.__males += 1
        if self.__sex == "女":
            Person.__females += 1
    def get_number_male(self):
        return self.__males
    def get_number_females(self):
        return self.__females


person = Person("仙女1",20,"女")
person1 = Person("仙女2",20,"男")
person2 = Person("仙女3",20,"女")
person3 = Person("仙女4",20,"男")
person4 = Person("仙女5",20,"女")
person5 = Person("仙女6",20,"男")
# print(Person.__dict__)
print("男生数是:%s" % person.get_number_male())
print("女生数是:%s" % person.get_number_females())

你可能感兴趣的:(1.设计一个Person,包含姓名、年龄和性别三个私有成员属性,另外Person类还包含males和females两个私有类属性,用来记录男生和女生的数量, 可以通过number_male和numb)