代码之家  ›  专栏  ›  技术社区  ›  Error_2646

处理好对象类型的困惑

  •  0
  • Error_2646  · 技术社区  · 7 年前

    这可能很明显,但我对这个还很陌生。

    如果这类问题有什么好的学习材料,我也会很感激的。

    例如,如果对象模型是Person Minor/Regular/Senior

    class Person:
        def __init__(self, full_name, age):
            self.species = "human"
            self.age = age
            self.first_name = full_name.split()[0]
            self.last_name = full_name.split()[1]
            self.age_classification = self.classify_age(self.age)
    
        def classify_age(self, input_age):
            if input_age < 18:
                return 'minor'
            elif input_age <= 64:
                return 'regular adult'
            elif input_age > 64:
                return 'senior'
    
    class Minor(Person):
        def __init__(self):
            self.number_older_siblings = 0
            self.number_younger_siblings = 0
    
        def is_unloved(self):
            if self.number_older_siblings > 0 and self.number_younger_siblings > 0:
                return True
            else:
                return False
    

    有没有干净的方法来声明一个人,这样如果他不满18岁,他就会“自动”成为一个子类型的对象?

    你不必这么做

    my_person = Person('John Smith', 15)
    
    if my_person.age_classification == 'minor':
        my_person = Minor()
        my_person.number_older_siblings = 1
        my_person.number_younger_siblings = 1
        print(my_person.is_unloved())
    

    但是如果声明满足一个条件,就可以推断出它

    my_person = Person('John Smith', 15)
    
    my_person.number_older_siblings = 1
    my_person.number_younger_siblings = 1
    print(my_person.is_unloved())
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   skrrgwasme    7 年前

    我认为莫伊拉·琼斯的解决方案是好的,但你可以更进一步:

    class Person:
    
        @staticmethod
        def create(full_name, age):
            if age <= 18:
                return Minor()
            else:
                return Person(full_name, age)
    
        def __init__(self, full_name, age):
            self.species = "human"
            self.age = age
            self.first_name = full_name.split()[0]
            self.last_name = full_name.split()[1]
            self.age_classification = self.classify_age(self.age)
    
        def classify_age(self, input_age):
            if input_age < 18:
                return 'minor'
            elif input_age <= 64:
                return 'regular adult'
            elif input_age > 64:
                return 'senior'
    
    class Minor(Person):
        def __init__(self):
            self.number_older_siblings = 0
            self.number_younger_siblings = 0
    
        def is_unloved(self):
            if self.number_older_siblings > 0 and self.number_younger_siblings > 0:
                return True
            else:
                return False
    
    print(type(Person.create("John Smith", 19)))
    print(type(Person.create("Bob Smith", 12)))
    

    输出:

    <
    <类“\u main\u Minor”>

    create() 作为父类的静态方法,我们可以像Moira建议的函数一样使用它,但是它可以与类定义共存,而不是作为一个独立浮动的函数。我认为这使得函数和它所操作的对象之间的关系更加清晰。注意,静态方法不接收 self 参数,就像其他对象方法一样。

        2
  •  4
  •   Moira Jones    7 年前

    Person

    def create_person(full_name, age):
        if age <= 18:
            return Minor()
        else:
            return Person(full_name, age)
    

    然后:

    >>> type(create_person("John Smith", 19))
    __main__.Person
    
    >>> type(create_person("Bob Smith", 12))
    __main__.Minor
    

    另外,您可能需要为 Minor full_name age .