如何使用Python创建继承的对象?

阿尔内特

我有以下嵌套的字典:

world = {'europe' :
                     {'france' : ['paris', 'lion'],
                      'uk' : ['london', 'manchester']}},
         {'asia' :
                     {'china' : ['beijing'],
                     {'japan' : ['tokyo']}}

我正在尝试以下对象:

class world:
    continents = {} # dict of continents

class continent(world):
    name = '' # name of the continent
    countries = {} # dict of countries

class country(continent):
    name = '' # name of the country
    cities = [] # list of cities

class city(country):
    name = '' # name of the city

目标是countriescontinent对象中获取所有内容,或者对象中获取country和的continent名称city

用Python做到这一点的最佳方法是什么?

朋奇亚

从句法上讲,这些类应定义为

class World(object):
    def __init__(self, continents):
        self.continents = continents

class Continent(World):
    def __init__(self, name):
        self.name = '' # name of the continent
    ...

class Country(Continent):
    def __init__(self, name):
        self.name = '' # name of the country
    ...

class City(Country):
    def __init__(self, name):
        self.name = '' # name of the city
    ...

但是,在这种情况下,这没有任何意义。

子类化意味着其他:

Class Animal(object):
    pass

Class Dog(Animal):
    pass

Class Snake(Animal):
    pass

狗是一种特殊的动物。狗是动物。蛇也是动物。

就您而言,大陆不是世界的类型,国家不是大陆的类型,依此类推。

相反,您想关联这些类,这些类可以作为单独的类存在,也可以在另一个内部使用。

例如

class City(object):
    def __init__(self, name):
        self.name = '' # name of the city

class Country(object, cities):
    def __init__(self, name, cities):
        self.name = name # name of the country
        self.cities = cities # it's a list of Cities instances

class Continent(object):
    def __init__(self, name, countries):
        self.name = name # name of the continent
        self.countries = countries # it's a list of Countries instances

class World(object):
    def __init__(self, continents):
        self.continents = continents # it's a list of Continent instances

france = Country('France', [City('Paris'), City('Annecy'), City('St. Tropez')])
italy = Country('Italy', [City('Rome'), City('Milan')])
uk = Country('UK', [City('London'), City('Bath')])

europe = Continent('europe', [france, italy, uk])
...

注意:以上仅是示例。由于多种原因,这可能不是在python中执行此操作的最佳方法,具体取决于您打算如何操作这些对象。

这是一个广泛而漫长的主题。

我建议在网上查找有关对象定向(也称为面向对象编程的OOP或面向对象设计的OOD)的良好教程。

这是一个教程,但是有成千上万的在线内容。

之后,您将能够设计对象应公开的接口,以便在本地/应用程序级别提供某些功能。

提示:使用RDBM(关系数据库管理系统)将帮助您关联和管理模型。了解有关ERD(实体关系图)的信息,以帮助您设计数据模型。

:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用LESS创建继承?

来自分类Dev

如何创建从父字段继承的对象的实例?

来自分类Dev

如何创建继承的类对象的数组?

来自分类Dev

使用Linq Select创建新的继承对象

来自分类Dev

使用bless创建具有继承的对象

来自分类Dev

如何创建和使用存储为c ++对象的python对象?

来自分类Dev

使用多继承构建python对象

来自分类Dev

使用txtfile创建从dict继承的类-python

来自分类Dev

使用继承从Python类创建JSON

来自分类Dev

如何为继承多个接口的对象创建模拟接口

来自分类Dev

Java:如何创建从继承的类中获取对象的getter方法?

来自分类Dev

如何创建仅继承特定属性的对象的别名

来自分类Dev

Java:如何创建从继承的类中获取对象的getter方法?

来自分类Dev

使用多态对象创建时发生继承错误

来自分类Dev

如何从继承访问对象

来自分类Dev

如何从继承访问对象

来自分类Dev

如何在python中使用循环创建多个类对象?

来自分类Dev

Python-如何使用Kwargs从类创建对象或调用方法

来自分类Dev

如何使用python中的继承将许多对象连接为一个对象?(在运行时)

来自分类Dev

使用继承的对象列表

来自分类Dev

使用oracle对象继承

来自分类Dev

使用继承的对象列表

来自分类Dev

Python:如何创建“全局对象”?

来自分类Dev

如何从python数组创建对象

来自分类Dev

Python:如何加快对象的创建?

来自分类Dev

如何使用for循环创建对象

来自分类Dev

如何使用参数创建对象

来自分类Dev

如何使用JSON创建对象?

来自分类Dev

如何使用for循环创建对象