我正在尝试使用针对Nexus的Android Studio。到目前为止,它很棒,我发现从我的.NET舒适区迁移到相当无缝。直到:
在.NET中我常常使用树遍历模式,其中树中的每个节点都是从公共基类派生的类的实例。我打电话给' foo()' root的函数,它调用每个后代的foo(),因此调用会在树周围传播。很标准真的[博士邪恶的声音]。
在java中实现这个似乎已经成功了。下面的代码不能在java中工作但在.NET中工作,我不知道为什么。如果我在我的任何一个句柄中放置断点。实现,没有一个被调用。如果我"踏入"打电话给孩子' Handle'实现,它变得非常疯狂,并且没有异常报告就停止了程序。
这应该有用吗?谢谢你的任何想法。
// *************
// BASE CLASS WITH AddChild, Handle
// *************
public class cA
{
public int m_Tag = 0;
protected cA ptr_FirstChild = null;
protected cA ptr_LastChild = null;
protected cA ptr_Parent = null;
protected cA ptr_NextSibling = null;
public cA(int tag)
{
m_Tag = tag;
}
public void AddChild(cA a)
{
a.ptr_Parent = this;
if (ptr_FirstChild == null)
{
ptr_FirstChild = a;
ptr_LastChild = a;
}
else
{
ptr_LastChild.ptr_NextSibling = a;
ptr_LastChild = a;
}
}
public void Handle()
{
int a;
a=3;
cA tmp = ptr_FirstChild;
while (tmp!= null)
{
tmp.Handle();
tmp = tmp.ptr_NextSibling;
}
}
}
// *************
// DERIVED CLASS, overrides Handle
// *************
public class cB extends cA
{
public cB(int tag)
{
super(tag);
}
@Override
public void Handle()
{
int a;
a=4;
cA tmp = ptr_FirstChild;
while (tmp!= null)
{
tmp.Handle();
tmp = tmp.ptr_NextSibling;
}
}
}
// *************
// Usage of classes
// build a tree with both cAs and cBs,
// a cB as a root, with two child cAs. Then call
// root.Handle, hoping to traverse the tree.
// *************
cA a1 = new cA(1); // instantiate all leaves
cA a2 = new cA(2);
cB b1 = new cB(1);
b1.AddChild(a1); // build the tree
b1.AddChild(a2);
b1.Handle(); // call Handle on the root, intending to traverse the tree, but this halts the program
答案 0 :(得分:1)
当代码在我的机器上运行并遍历树时,我可能会想到一个问题是断点的位置,因为当handle
的第一行是int a;
时,设置一个断点此行不会导致中断,因为没有初始化的纯声明不是有效的断点位置。相反,在第二行a = 4;
上设置断点实际上会导致中断。