碰巧看到吕震宇 兄的文章 C#设计模式(1) ,中提到 先有鸡还是先有蛋 的代码
 using System;
using System;

 class Client
class Client
 {
{
 public static void Main ()
   public static void Main ()
 {
   {
 Base b = new Base();
      Base b = new Base();
 Derived d = new Derived();
      Derived d = new Derived();
 b.d = d;
      b.d = d;
 Console.WriteLine(b.d.m);
      Console.WriteLine(b.d.m);      
 }
   }
 }
}

 class Base
class Base
 {
{
 public int n = 9;
   public int n = 9;
 public Derived d;
   public Derived d;
 }
}

 class Derived : Base
class Derived : Base
 {
{
 public int m = 10;
   public int m = 10;   
 }
        “Derived继承自Base,可以说没有Base就没有Derived,可Base里面有一个成员是Derived类型。到底是先有鸡还是先有蛋?这个程序可以正常编译执行并打印结果10。”
}
        “Derived继承自Base,可以说没有Base就没有Derived,可Base里面有一个成员是Derived类型。到底是先有鸡还是先有蛋?这个程序可以正常编译执行并打印结果10。”
     
上面提到可以正常编译并能打印结果,是没错,不过这不能说明先有鸡还是先有蛋这个问题。先有鸡还是先有蛋是说是鸡先生蛋呢还是蛋生鸡。在上面的代码中Base类声明有一个Derrived类,在Base实例化时,也就是“生”的时候并没有把Derrived类也“生”出来,因此不能说明问题.
Base类声明有一个Derrived类,也就是Base类的实例有一个指向Derrived类的指针,这是个引用问题,而这个引用在Base类创建时为null。
看下面的代码:
 using System;
using System;

 class Client
class Client
 {
{
 public static void Main ()
    public static void Main ()
 {
    {
 Base b = new Base();
        Base b = new Base();
 Derived d = new Derived();
        Derived d = new Derived();
 b.d = d;
        b.d = d;
 Console.WriteLine(b.d.m);
        Console.WriteLine(b.d.m);      
 }
    }
 }
}

 class Base
class Base
 {
{
 public Base()
    public Base()
 {
    {
 this.d = new Derived();
        this.d = new Derived();
 }
    }
 public int n = 9;
    public int n = 9;
 public Derived d;
    public Derived d;
 }
}

 class Derived : Base
class Derived : Base
 {
{
 public Derived() : base()
    public Derived() : base()
 {
    {
 }
    }
 public int m = 10;
    public int m = 10;   
 }
         在上面的代码中Base类声明有一个Derrived类,在Base实例化时,也就是“生”的时候把Derrived类也“生”出来,编译通过,运行的时候死循环!
}
         在上面的代码中Base类声明有一个Derrived类,在Base实例化时,也就是“生”的时候把Derrived类也“生”出来,编译通过,运行的时候死循环!
我不知道有什么托管对象可以先在Object对象之前创建出来!
Object对象作为所有其他对象的根对象,自然Object对象先创建出来,也就是说先有Object再有其他派生对象。
 using System;
using System;
 class Client
class Client {
{ public static void Main ()
   public static void Main () {
   { Base b = new Base();
      Base b = new Base(); Derived d = new Derived();
      Derived d = new Derived(); b.d = d;
      b.d = d; Console.WriteLine(b.d.m);
      Console.WriteLine(b.d.m);       }
   } }
}
 class Base
class Base {
{ public int n = 9;
   public int n = 9; public Derived d;
   public Derived d; }
}
 class Derived : Base
class Derived : Base {
{ public int m = 10;
   public int m = 10;    }
}上面提到可以正常编译并能打印结果,是没错,不过这不能说明先有鸡还是先有蛋这个问题。先有鸡还是先有蛋是说是鸡先生蛋呢还是蛋生鸡。在上面的代码中Base类声明有一个Derrived类,在Base实例化时,也就是“生”的时候并没有把Derrived类也“生”出来,因此不能说明问题.
Base类声明有一个Derrived类,也就是Base类的实例有一个指向Derrived类的指针,这是个引用问题,而这个引用在Base类创建时为null。
看下面的代码:
 using System;
using System;
 class Client
class Client {
{ public static void Main ()
    public static void Main () {
    { Base b = new Base();
        Base b = new Base(); Derived d = new Derived();
        Derived d = new Derived(); b.d = d;
        b.d = d; Console.WriteLine(b.d.m);
        Console.WriteLine(b.d.m);       }
    } }
}
 class Base
class Base {
{ public Base()
    public Base() {
    { this.d = new Derived();
        this.d = new Derived(); }
    } public int n = 9;
    public int n = 9; public Derived d;
    public Derived d; }
}
 class Derived : Base
class Derived : Base {
{ public Derived() : base()
    public Derived() : base() {
    { }
    } public int m = 10;
    public int m = 10;    }
}我不知道有什么托管对象可以先在Object对象之前创建出来!
Object对象作为所有其他对象的根对象,自然Object对象先创建出来,也就是说先有Object再有其他派生对象。
 
                    
                     
                    
                 
                    
                 
 
        

 
    
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号