这是事情:
我正在使用CommonJS方式使我的移动(iPhone/Android)应用程序模块化.这并不奇怪.但有一点我无法理解.
CommonJS让我创建了STATIC私有变量,这让我可以轻松创建单例.至少,我认为这是因为获取require()d 的文件的内容只读取一次,然后每次都返回export对象(只初始化一次).
但是当我创建如下所示的循环引用时,每次都会执行包含模块内的代码.
等等......
有趣的是,在我写这个问题的时候,我突然意识到require()在下一个问题开始之前没有任何调用完成(因此下面显示了堆栈溢出).
关于我是否正常的任何想法?这是凌晨5点过来的所以,所以我所关注的所有赌注都是关闭的:D.
例子:
看到这段代码,它定义了一个单例:
/* Singleton.js */
exports.getSingleton = getSingleton;
function getSingleton(name) {
if (!instance) {
instance = new Thing(name);
}
return instance;
}
function Thing(name) {
this.name = name;
}
var instance;
Run Code Online (Sandbox Code Playgroud)
我require()这个文件是这样的:
var theFirstThing = require('Singleton').getSingleton('first');
Ti.API.info('first: ' + theFirstThing.name)
var possiblyAnotherOtherThing = require('Singleton').getSingleton('second');
Ti.API.info('second: ' + possiblyAnotherOtherThing.name);
Run Code Online (Sandbox Code Playgroud)
输出是:
[DEBUG] loading: /path/to/sim/MyApp.app/app.js, resource: app_js
[DEBUG] loading: /path/to/sim/MyApp.app/Singleton.js, resource: Singleton_js
[INFO] first: first
[INFO] second: first
Run Code Online (Sandbox Code Playgroud)
为什么那样的循环引用如下所示不起作用?(如果你愿意的话,我可能已经自己研究了这个,评论/回答). …
我正在学习Elixir,直到第7章PragProg书,在考虑了不变性和其他项目后,我认为通常不可能在Elixir Maps/Tuples/Lists等中创建循环引用.其中A - > B - > C - > A.
没有真正试图欺骗系统,这是真的吗?
我正在编写自己的IFormatter实现,我想不出一种方法来解决两个实现ISerializable的类型之间的循环引用.
这是通常的模式:
[Serializable]
class Foo : ISerializable
{
private Bar m_bar;
public Foo(Bar bar)
{
m_bar = bar;
m_bar.Foo = this;
}
public Bar Bar
{
get { return m_bar; }
}
protected Foo(SerializationInfo info, StreamingContext context)
{
m_bar = (Bar)info.GetValue("1", typeof(Bar));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("1", m_bar);
}
}
[Serializable]
class Bar : ISerializable
{
private Foo m_foo;
public Foo Foo
{
get { return m_foo; }
set { m_foo = value; }
}
public …Run Code Online (Sandbox Code Playgroud) 问题:我想将两个类拆分为自己的文件,同时避免循环引用.
我有一个单元有一些类(和一些枚举和常量).任何人都会认出Click and Clack挺杆兄弟:
unit Cartalk;
interface
type
TSolution = (solTransmission, solBrakes, solGremlins);
TTappetBrother = class(TObject)
public
function GetSolution: TSolution; virtual; abstract;
end;
TClick = class(TTappetBrother)
public
function GetSolution: TSolution; override;
end;
TClack = class(TTapperBrother)
public
function GetSolution: TSolution; override;
end;
implementation
function TClick.GetSolution: TSolution;
begin
Result := solTransmission;
end;
function TClack.GetSoltution: TSolution;
begin
Result := solGremlins;
end;
end.
Run Code Online (Sandbox Code Playgroud)
现在显然是我的两个班级TClick并且TClick相当复杂.对于管理,我想分裂TClick和TClack出自己的单位而没有违反任何现有的外部代码.
我的第一个问题是:
unit Cartalk;
interface
uses
Cartalk_Click, Cartalk_Clack; …Run Code Online (Sandbox Code Playgroud) delphi refactoring circular-dependency delphi-5 circular-reference
我在AfterSignup单元中使用BeforSignup,以便能够从AfterSignup代码中调用电子邮件变量,最后我知道了一个问题,因为我想创建一个按钮,使用代码打开AfterSignup窗口:
AfterSignup.Show;
Run Code Online (Sandbox Code Playgroud)
但问题是我有义务将AfterSignup单元添加到BeforeSignup的使用列表中,这正是我无法做到的,因为我很喜欢使用BeforeSignup到AfterSignup单元.
我收到一个错误说,圆形单位的参考.
JVM能否找到大型循环引用并收集对象?有没有官方文件/链接说明相同?或者你有大型循环引用的垃圾收集的任何好/坏经验.
更新链接:http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#997442
我已经在这里多次询问过这个关于我正在尝试制作的这个红润游戏的问题.我正在研究基于文本的冒险游戏.首先我用Java创建它,因为这就是我在学习游戏所用的类.现在我正在尝试学习需要objective-c的iOS开发.在参加Lynda Essentials课程之后,我对目标c感到非常满意(当然,以前的Java经验帮助过).无论如何我正在研究这个游戏,我遇到的问题似乎与目标c非常不同.
在Java中,当我有多个类时,他们只需要在同一个目录中,以便我在其他类中使用它们.在Objective-C中不是这种情况......如果我想在B类中使用A类,我必须导入头文件.对于这个游戏,我有两个自定义类,一个Location类和一个Exit类.Location类需要知道它有什么退出(所以如果我想使用它我必须导入Exit.h)并且退出需要知道它连接到哪个位置(所以我必须导入Location.h).似乎我不能这样做,因为有一种称为循环引用(或类似的东西).但是,如果我不这样做,那么我会收到"预期的类型"错误.所以我不知道该怎么做.我将在下面显示代码.
Exit.h
#import <Foundation/Foundation.h>
#import "Location.h"
#define NORTH 0
#define SOUTH 1
#define EAST 2
#define WEST 3
@interface Exit : NSObject
@property NSString * dirName;
@property NSString * dirShortName;
@property int direction;
@property Location * connection;
-(id)initWithConnection:(Location *) loc andDirection:(int) dir;
@end
Run Code Online (Sandbox Code Playgroud)
Exit.m
#import "Exit.h"
@implementation Exit
@synthesize dirName;
@synthesize dirShortName;
@synthesize direction;
@synthesize connection;
-(id)initWithConnection:(Location *)loc andDirection:(int)dir {
self = [super init];
if(self) {
direction = dir;
switch(direction) {
case 0:
dirName = @"North";
dirShortName = …Run Code Online (Sandbox Code Playgroud) 假设我有两个记录:人与动物.每条记录都在一个单独的包中.
套餐人员:
with animals;
use animals;
package persons is
type person is record
...
animalref: animalPOINTER;
...
end record;
type personPOINTER is access person;
end persons;
Run Code Online (Sandbox Code Playgroud)
包装动物:
with persons;
use persons;
package animals is
type animal is record
...
ownerref: personPOINTER;
...
end record;
type animalPOINTER is access animal;
end animals;
Run Code Online (Sandbox Code Playgroud)
我在这里有循环单元依赖,编译器会产生致命错误.
有没有人有解决这个问题的模式?
谢谢!
我在Delphi 10中有一个三角形网格结构.
出于性能原因,我将网格顶点,三角形面等的数据存储在TList的后代中.
我让TLists为列表的每个成员进行计算.对于这些计算,我需要访问TMesh结构的某些字段.因此,在创建TMesh并随后创建列表期间,我将父TMesh分配给列表.我使用TMesh的前向声明来做到这一点.请参阅以下代码:
type
{forward declaration}
TMesh=class;
TVertex=record
Point: TPoint3D;
//other fields
end;
TVertices=class(TList<TVertex>)
Mesh: TMesh;
procedure DoSomethingWithAllVertices; //uses some fields of TMesh
constructor Create(const AMesh: TMesh);
//other methods
end;
TTriangleFace=record
Vertices: Array[0..2] of Integer;
//other fields
end;
TTriangleFaces=class(TList<TTriangleFace>)
Mesh: TMesh;
procedure DoSomethingWithAllTriangleFaces; //uses some fields of TMesh
constructor Create(const AMesh: TMesh);
//other methods
end;
TMesh=class(TComponent)
Vertices: TVertices;
TriangleFaces: TTriangleFaces;
constructor Create(AOwner: TComponent);
//other fields & methods
end;
implementation
constructor TMesh.Create(AOwner: TComponent);
begin
inherited;
Vertices:=TVertices.Create(Self);
TriangleFaces:=TTriangleFaces.Create(Self);
end;
constructor TVertices.Create(const …Run Code Online (Sandbox Code Playgroud) 想象一下以下组成的例子:
public enum Hand {
ROCK(SCISSORS),
PAPER(ROCK),
SCISSORS(PAPER);
private final Hand beats;
Hand(Hand beats) {
this.beats = beats;
}
}
Run Code Online (Sandbox Code Playgroud)
我将收到Illegal forward reference前向引用的错误SCISSORS.
有没有办法在Java中处理这样的前向引用?
或者你会如何模拟这种情况,你在几个枚举值之间有一个逻辑循环引用?
delphi ×3
java ×2
.net ×1
ada ×1
algorithm ×1
appcelerator ×1
commonjs ×1
delphi-2010 ×1
delphi-5 ×1
elixir ×1
enums ×1
jvm ×1
objective-c ×1
refactoring ×1
titanium ×1