如何从python中的单个类继承一个类的多个实例 [英] How to inherit multiple instances of a class from a single class in python

查看:80
本文介绍了如何从python中的单个类继承一个类的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我是编程的新手,刚刚开始学习类和继承,所以也许是由于我缺乏理解才导致我遇到问题?

Disclaimer: I am new to programming and have just started learning about Classes and Inheritance so perhaps it is my lack of understanding which is causing me issues?

我在单独的文件 person.py address.py

人类从地址类继承,但是一个人可以有多个地址(邮政,物理等)。如何继承多个人我的目标是最终为将来的项目建立一个通用类库,因此有关该概念的任何帮助或建设性评论都将是我们的不二选择。

The person class inherits from the address class however a person can have multiple addresses (postal, physical etc..) How can I inherit multiple instances of my address class to for each type.

赞赏。

我希望能够创建以下代码,其中邮政地址和实际地址都使用 Address 类:

I want to be able to create something like the below code where both the postal and the physical address utilise the Address class:

employee = Person()
employee.set_postal_address("342 Gravelpit Terrace","Bedrock")
employee.set_physical_address("742 Evergreen Tce", "Springfield")

下面是我的代码,我没有在 Person()类中创建对 Address()的任何引用不确定如何实现?

Below is my code, I have not created any reference to Address() in the Person() class as I am not sure how to achieve this?

# address.py

class Address:
    def __init__(self):
        self.__line1 = None
        self.__line2 = None
        self.__town_city = None
        self.__post_code = None
        self.__state_region = None
        self.__country = None

    # Line 1
    def get_line1(self):
        return self.__line1

    def set_line1(self, line1):
        self.__line1 = line1

    #etc....



# person.py

from address import * 

class Person(Address):

    def __init__(self):
        self.__first_name = None
        self.__last_name = None
        self.__postal_address = []
        self.__physical_adress = []


    def set_postal_address(self):
        # use the address class 
        # return a list of address values
        self.__postal_address = []

    def set_physical_address(self):
        # use the address class 
        # return a list of address values
        self.__physical_address = []  

    #etc...

任何帮助都将不胜感激。如果有更好的处理方法,请告诉我。

Any help is greatly appreciated. If there is a better way of handling this please let me know.

推荐答案

创建类时,您不必继承 Address 人员。实现目标的一种方法是在 Person 类中初始化 Address 类对象。

You do not need to necessarily inherit Address when creating the class Person. One way to achieve your goal is to initialize an Address class object within the Person class.

# address.py

class Address:
    def __init__(self, **kwargs):
        self.__line1 = kwargs.get('__line1')
        self.__line2 = kwargs.get('__line2')
        self.__town_city = kwargs.get('__town_city')
        self.__post_code = kwargs.get('__post_code')
        self.__state_region = kwargs.get('__state_region')
        self.__country = kwargs.get('__country')

    # Line 1
    def get_line1(self):
        return self.__line1

    def set_line1(self, line1):
        self.__line1 = line1

    #etc....



# person.py

from address import * 

class Person:

    def __init__(self, **kwargs):
        self.__first_name = kwargs.get('__first_name')
        self.__last_name = kwargs.get('__last_name')
        self.__postal_address = None
        self.__physical_address = None


    def set_postal_address(self, **kwargs):
        # use the address class 
        self.__postal_address = Address(**kwargs)

    def set_physical_address(self, **kwargs):
        # use the address class 
        self.__physical_address = Address(**kwargs)

    #etc...

这使您可以避免继承的方法在给定的类对象中混乱,但仍可以保持良好的类结构。

This allows you to keep inherited methods from getting cluttered in a given class object but still allows you to keep nice class structures.

然后您可以执行类似的操作:

Then you can do something like:

bob=Person(__first_name='Bob', __last_name='Smith')
bob.set_postal_address(__line1='42 Wallaby Way', __town_city='Sydney')
# Since we are using kwargs.get() any not specified Kwarg comes out as None.
# Now `__postal_address` is its own Address class and comes with Address methods
bob.__postal_address.set__line2('Apt 3B')

现在有时您想从更高级别的类继承通用方法。您可以在此处阅读有关python类的更多信息:( https://docs.python.org/ 3.6 / tutorial / classes.html

Now there are times when you would want to inherit common methods from higher level classes. You can read more on python classes here: (https://docs.python.org/3.6/tutorial/classes.html)

这篇关于如何从python中的单个类继承一个类的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆