可有人請敷衍我詳細下列情況下,這將是,如果解釋爲包括三種情況下的內存分配和引用更方便:爲什麼以下案件的執行流程不同?
- 如何流動在三種情況下執行?
- 爲什麼這三種情況下的流程不同?
- 儘管兩個類之間存在循環依賴關係,但爲什麼僅在單個案例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