如果两个模块相互导入会发生什么?
为了概括这个问题,Python中的循环导入怎么样?
我最近一直在使用nodejs并且仍然掌握模块系统,所以如果这是一个显而易见的问题,请道歉.我想要的代码大致如下所示:
a.js(主节点与节点一起运行)
var ClassB = require("./b");
var ClassA = function() {
this.thing = new ClassB();
this.property = 5;
}
var a = new ClassA();
module.exports = a;
Run Code Online (Sandbox Code Playgroud)
b.js
var a = require("./a");
var ClassB = function() {
}
ClassB.prototype.doSomethingLater() {
util.log(a.property);
}
module.exports = ClassB;
Run Code Online (Sandbox Code Playgroud)
我的问题似乎是我无法从ClassB的实例中访问ClassA的实例.
是否有正确/更好的方法来构建模块以实现我想要的?有没有更好的方法在模块之间共享变量?
我正在阅读Scott Meyers的"Effective C++"一书.有人提到有内置指针tr1::shared_ptr和tr1::weak_ptr行为,但它们跟踪tr1::shared_ptrs指向对象的数量.
这称为引用计数.这在防止非循环数据结构中的资源泄漏方面效果很好,但是如果两个或多个对象包含tr1::shared_ptrs形成循环,则循环可以使彼此的引用计数保持在零以上,即使所有指向循环的外部指针都已被破坏.
那是tr1::weak_ptrs进来的地方.
我的问题是循环数据结构如何使引用计数高于零.我恳请一个示例C++程序.问题是如何解决的weak_ptrs?(再次,请举例).
有没有办法解决因两个相互引用的枚举导致的类加载问题?
我有两组枚举,Foo和Bar,定义如下:
public class EnumTest {
public enum Foo {
A(Bar.Alpha),
B(Bar.Delta),
C(Bar.Alpha);
private Foo(Bar b) {
this.b = b;
}
public final Bar b;
}
public enum Bar {
Alpha(Foo.A),
Beta(Foo.C),
Delta(Foo.C);
private Bar(Foo f) {
this.f = f;
}
public final Foo f;
}
public static void main (String[] args) {
for (Foo f: Foo.values()) {
System.out.println(f + " bar " + f.b);
}
for (Bar b: Bar.values()) {
System.out.println(b + " foo " + b.f);
}
} …Run Code Online (Sandbox Code Playgroud) 我有以下代码片段:
public class Example {
private Integer threshold;
private Map<String, Progress> history;
protected void activate(ComponentContext ctx) {
this.history = Collections.synchronizedMap(new LinkedHashMap<String, Progress>() {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Progress> entry) {
return size() > threshold;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
Theres是匿名LinkedHashMap类和Example类之间的循环依赖.这样可以吗?为什么不?它会被垃圾收集器很好地收回吗?
我查看了python的官方文档,但我似乎无法找到参考周期.任何人都可以澄清它对我来说是什么,因为我正在尝试理解GC模块.提前感谢您的回复.
python garbage-collection reference-counting cyclic-reference
这是我的一些代码的简化版本:
public struct info
{
public float a, b;
public info? c;
public info(float a, float b, info? c = null)
{
this.a = a;
this.b = b;
this.c = c;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是Struct member 'info' causes a cycle in the struct layout.我在结构之后的错误,就像值类型行为一样.我可以使用类和克隆成员函数来模拟这个,但我不明白为什么我需要.
这个错误怎么样?在某些类似的情况下,递归可能会永远导致构造,但在这种情况下我无法想到它的任何方式.下面是程序编译时应该没问题的例子.
new info(1, 2);
new info(1, 2, null);
new info(1, 2, new info(3, 4));
Run Code Online (Sandbox Code Playgroud)
编辑:
我使用的解决方案是使"info"成为一个类而不是一个struct,并给它一个成员函数来返回我在传递它时使用的副本.实际上模拟与结构相同但具有类的行为.
我在寻找答案时也创建了以下问题.
我刚刚阅读了Mark Lutz的"学习Python",并且遇到了这个代码示例:
>>> L = ['grail']
>>> L.append(L)
>>> L
['grail', [...]]
Run Code Online (Sandbox Code Playgroud)
它被确定为循环数据结构.
所以我很想知道,这是我的问题:
似乎有点混乱,我认为这源于非常简短的代码示例...这里有几行使用相同的对象L
>>> L[0]
'grail'
>>> L[1][0]
'grail'
>>> L[1][1][0]
'grail'
Run Code Online (Sandbox Code Playgroud) 我搜索并找到了有关此主题的一些信息,但答案要么令人困惑,要么不适用.
我有这样的事情:
class Thing (val name:String, val refs:IndexedSeq[Ref])
class Ref (val name:String, val thing:Thing)
Run Code Online (Sandbox Code Playgroud)
现在,我想说,加载一个文件,解析它并从中填充这个数据结构.它是不可变的和循环的,怎么会这样做?
另外,假设我确实填充了这个数据结构,现在我想修改它,比如更改rootThing.refs(3).name,怎么可能这样做呢?
感谢此处发布的想法.在这一点上,我在想如果一个人真的想要这样的东西的持久性数据结构,那就跳出框框思考并考虑客户端代码需要问什么问题.因此,不要考虑对象和字段,而应考虑查询,索引等.首先,我在想: 是否存在双向多图持久数据结构?
我正在尝试使用parsec库在haskell中实现简单的解析器(用于学习目的).所以我写了一堆数据结构和相关的函数,如下所示:
data SourceElement
= StatementSourceElement Statement
| FunctionSourceElement FunctionName FunctionBody
data Statement
= IfStatement Expr Statement Statement
| WhileStatement Expr Statement
data FunctionBody = FunctionBody [SourceElement]
parseSourceElement :: Parser SourceElement
parseSourceElement = ...
parseFunctionBody :: Parser FunctionBody
parseFunctionBody = ...
Run Code Online (Sandbox Code Playgroud)
它工作正常.现在我想将这些东西分成两个模块来分离FunctionBody和Statement数据结构(因为可读性问题).但我不能!原因是SourceElement和之间的循环依赖FunctionBody.
那么,有什么方法可以解决这个问题吗?
cyclic-reference ×10
python ×3
java ×2
c# ×1
c++ ×1
c++11 ×1
classloader ×1
constructor ×1
cyclic-graph ×1
enums ×1
haskell ×1
immutability ×1
member ×1
module ×1
node.js ×1
oop ×1
recursion ×1
require ×1
scala ×1
shared-ptr ×1
struct ×1
weak-ptr ×1