- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是一个处理多重组合的玩具问题:
有两个对象类,代表弹珠和盒子。 Marble 总是包含在 Box 中,并且 Box 有一个类方法来表示当前在其中的弹珠。 Marble 一旦实例化,就应该能够传递给任何其他现有的 Box(或者甚至可能是另一个对象以实现可扩展性)。
在 Python 中实现多个“has-a”组合的最佳模式是什么? (我发现了单个 has-a 示例,但没有偶然发现多个组合示例)。
我的第一个猜测,无论其值(value)如何,都是通过 Box 类中包含的方法(例如 create_marble、pass_marble、delete_marble 方法)处理 Marble 对象,并将 Marbles 列表作为 Box 类中的一个属性进行维护。但这真的是最好的方法吗?
最佳答案
class Marble(object):
def __init__(self,color=None):
self.color=color # I'm assuming this is necessary,
# just because "colored marbles in
# a box" is so typical
def __repr__(self):
return "Marble({})".format(self.color)
class Box(object):
def __init__(self,name=None,marbles=None):
self.name = name
if marbles is None:
marbles = list()
self.marbles = marbles
def addMarble(self,color=None):
self.marbles.append(Marble(color))
def giveMarble(self,other):
# import random
index = random.randint(0,len(self.marbles)-1)
try:
other.marbles.append(self.marbles.pop(index))
except AttributeError:
raise NotImplementedError("Can't currently pass marbles to an "
"object without a marbles list")
def __str__(self):
return '\n'.join([str(marble) for marble in self.marbles])
a = Box()
b = Box()
for _ in range(10): a.addMarble()
print(a)
# Marble(None)
# Marble(None)
# Marble(None)
# Marble(None)
# Marble(None)
# Marble(None)
# Marble(None)
# Marble(None)
# Marble(None)
# Marble(None)
a.giveMarble(b)
print(b)
# Marble(None)
关于Python 组合 : marbles in a box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23142386/
考虑这样的代码: trait Foo { fn foo(&self); } fn consume_func(b: Box>) { unimplemented!(); } fn prod
考虑这样的代码: trait Foo { fn foo(&self); } fn consume_func(b: Box>) { unimplemented!(); } fn prod
我不明白在 Rust 中,当我们 Box 父结构时,结构中的结构发生了什么。 struct Outer1 { child: Inner1, } struct Inner1 { n: i
我将开始开发一个新网站,并准备处理浏览器用于计算元素宽度和高度的不同方法 (box model stuff)。不知何故,我想到了:如果我只是将 box-sizing 应用于网站中的所有元素会怎么样?
在 java 应用程序中使用 box java sdk 访问 box api 时,我遇到了下面提到的错误。请指出此问题的原因。 Exception in thread "main" com.box.s
计算盒子的宽高时,outline width like outline: 5px dashed red; 如果 box-sizing:border-box,框的宽度/高度是否会考虑轮廓? 最佳答案 我
读完the subtyping chapter of the Nomicon ,我无法理解类型参数的协方差。特别是对于 Box类型,描述为:T is covariant . 但是,如果我写这段代码:
给定这段代码: trait Trait {} struct Child; impl Trait for Child {} struct Father { child: &'a Box, } i
与文件 chooser for dropbox 类似,box.com 有没有? 最佳答案 Box 目前不维护自己的“文件选择器”;然而,有一个很棒的服务叫做 filepicker.io,它充当几乎所有
感谢您对我的问题的回复:Is this a bug of Box API v2 when getting events 这是一个与此相关的新问题。问题是我不能可靠地使用从以前的调用中获得的 next_
我试图让 Box Enterprise API 使用 As-User 工作。我有一个管理员帐户,我用它来尝试检索子帐户中的内容。 我首先使用管理员帐户检索子帐户的用户ID。并将用户 ID 添加为字段“
我想在一个简单的导轨中本地查看我的 Box 帐户中的图像 app .目标是使用这些图像进行幻灯片放映。我寻找必要的参数来传递每个图像相关 url成标签。看来我只能下载图像,或从 Box 应用程序中查看
这个问题在这里已经有了答案: Why doesn't Rust support trait object upcasting? (5 个答案) 关闭 4 年前。 给定代码 trait Base {
所以基本上我尝试从https://github.com/Gor-Ren/gym-jsbsim转换这个自定义健身房环境。使用farama基金会的gymnasium api。这是我正在处理的存储库:htt
我是BOX API的新手,因此正在使用API的v2。我正在从我的应用程序本地进行REST调用。 我想上传一个以前可能上传或未上传的文件。我知道父文件夹ID和文件名。我需要覆盖现有文件,或者至少要再
我已被授予访问(协作)文件夹的权限。我需要的是每天访问该文件夹并从中获取文件。现在我生成的开发者 token 将在 1 小时后过期。有什么办法可以得到authorization code没有第一站,它
我正在尝试将 Box 中上传的文件与另一个外部系统同步。我使用哪些 API 来获取“在给定 dd/mm/yy hh:mm:ss 后上传/更新的所有文件的列表”? 干杯! 最佳答案 我认为User Ev
Vagrant 文档和 CLI 使用术语“box”和“base box”。命名意味着基本框是一种特定类型的框,但是,这些术语似乎被用作同义词。 Vagrant“盒子”和 Vagrant“基础盒子”之间
Box 允许您为文件夹创建标签,但如何从 API 中获取该信息?我在 GET /folders/{id} API 中没有看到标签返回.标签也不被视为元数据。 最佳答案 如果你看看 Fields wri
我正在尝试确定应该使用哪个版本的 Box api。我的决定取决于 v1 api 的 EOL 时间线。我不想完成我的应用程序,就在应用程序发布之前(或之后不久),v1 api 停止工作。 我很乐意使用
我是一名优秀的程序员,十分优秀!