可有人请敷衍我详细下列情况下,这将是,如果解释为包括三种情况下的内存分配和引用更方便:为什么以下案件的执行流程不同?
- 如何流动在三种情况下执行?
- 为什么这三种情况下的流程不同?
- 尽管两个类之间存在循环依赖关系,但为什么仅在单个案例1失败时才执行案例1?
情况下,一个
namespace CircularDependency_1
{
class Program
{
static void Main(string[] args)
{
A a = new A();
Console.WriteLine("executed");
Console.ReadLine();
}
}
public class B
{
public static A a = new A();
public B()
{
Console.WriteLine("Creating B");
}
}
public class A
{
public static B b = new B();
public A()
{
Console.WriteLine("Creating A");
}
}
}
输出
Creating A
Creating B
Creating A
executed
情况下,两个
namespace CircularDependency_1
{
class Program
{
static void Main(string[] args)
{
A a = new A();
Console.WriteLine("executed");
Console.ReadLine();
}
}
public class B
{
public static A a;
public B()
{
a = new A();
Console.WriteLine("Creating B");
}
}
public class A
{
public static B b;
public A()
{
b = new B();
Console.WriteLine("Creating A");
}
}
}
OUTPUT 由于StackOverflowException,进程终止。
情况下为三个
namespace CircularDependency_1
{
class Program
{
static void Main(string[] args)
{
A a = new A();
Console.WriteLine("executed");
Console.ReadLine();
}
}
public class B
{
public A a;
public B()
{
a = new A();
Console.WriteLine("Creating B");
}
}
public class A
{
public B b;
public A()
{
b = new B();
Console.WriteLine("Creating A");
}
}
}
OUTPUT 过程由于StackOverflowException终止。
你为什么包含'java'标签? – SomeDude
@svasa我认为这个问题更多的是OOPS而不是面向语言 –
我投票结束这个问题作为题外话,因为[问题要求作业帮助必须包括你迄今为止所做的工作的总结问题以及你解决问题的难点描述](http://stackoverflow.com/help/on-topic)。 – RealSkeptic