C#之构造函数

构造函数分为:
1.实例构造函数
2.私有构造函数
3.静态构造函数
构造函数是一种特殊的方法,主要是为了给初始化对象赋初值。
1.实例构造函数

使用new
表达式创建某个类
的对象时,会使用实例构造函数创建和初始化所有实例成员变量。

   public
    
   class
    ProgramTest
    {
        
   int
    j;
        
   public
    ProgramTest()
        {
            j 
   =
    
   4
   ;
            Console.WriteLine(
   "
   I am ProgramTest,{0}
   "
   , 
   j
   );
        }
        
   static
    
   void
    Main(
   string
   [] args)
        {
   

               ProgramTest pt 
   =
    
   new
    ProgramTest();
            Console.Read();
        }

结果为:I am ProgramTest,4
在此次实验中,我们首先定义了一个私有成员j,经过初始化给他赋了一个初值4,当我实例化类ProgramTest时,就会执行实例构造函数。

诸如此类不带参数的构造函数叫“默认构造函数”,
如果某个类没有构造函数,则会自动生成一个默认构造函数,并使用默认值来初始化对象字段。

  public
   
  class
   ProgramTest
    {
        
  int
   j;
        
  public
   ProgramTest()
        {
            Console.WriteLine(
  "
  I am ProgramTest,{0}
  "
  , j);
        }
        
  static
   
  void
   Main(
  string
  [] args)
        {
            ProgramTest pt 
  =
   
  new
   ProgramTest();
            Console.Read();
        }

    }

  结果为:I am ProgramTest,
  0
    



也可以创建带有参数的构造函数


   
  public
   
  class
   ProgramTest
    {
        
  int
   j;
        
  public
   ProgramTest(
  int
   i)
        {
            j 
  =
   
  2
  ;
            Console.WriteLine(
  "
  I am ProgramTest,i={0},j={1}
  "
  ,i, j);
        }
        
  static
   
  void
   Main(
  string
  [] args)
        {
  

              ProgramTest pt 
  =
   
  new
   ProgramTest(
  1
  );
            Console.Read();
        }

结果为:I am ProgramTest i=1,j=
2

那若是一个类中既有无参构造函数,又有有参构造函数那会怎么样呢?

  public
   
  class
   ProgramTest
    {
        
  int
   j;
        
  public
   ProgramTest()
        {
            j 
  =
   
  3
  ;
            Console.WriteLine(
  "
  I am ProgramTest 默认构造函数,j={0}
  "
  , j);
        }
        
  public
   ProgramTest(
  int
   i)
        {
            j 
  =
   
  2
  ;
            Console.WriteLine(
  "
  I am ProgramTest 有参构造函数,i={0},j={1}
  "
  ,i, j);
        }
        
  static
   
  void
   Main(
  string
  [] args)
        {
  

              ProgramTest pt1 
  =
   
  new
   ProgramTest();
            ProgramTest pt2 
  =
   
  new
   ProgramTest(
  1
  );
            Console.Read();
        }

  

  结果为:I am ProgramTest 默认构造函数 j=
  3
       
  I am ProgramTest 有参构造函数 i=1,j=2

可见,若是两者同时存在,那么看类实例化时,是怎么实例的。
2.私有构造函数

私有构造函数是一种特殊的实例构造函数。
它通常用在只包含静态成员的类中。

如果类具有一个或多个私有构造函数而没有公共构造函数,则其他类(除嵌套类外)无法创建该类的实例。

      public
   
  class
   Test
    {
        
  private
    Test()
        {
            Console.WriteLine(
  "
  I am Test
  "
  );
        }
    }
    
  public
   
  class
   ProgramTest
    {
        
  int
   j;
        
  private
   ProgramTest()
        {
            j 
  =
   
  3
  ;
            Console.WriteLine(
  "
  I am ProgramTest 默认构造函数,j={0}
  "
  , j);
        }
        
  static
   
  void
   Main(
  string
  [] args)
        {
            Test t 
  =
   
  new
   Test();
            ProgramTest pt1 
  =
   
  new
   ProgramTest();
            Console.Read();
        }
    }

结果:这时你会发现编译器会提示你,你无法创建Test的实例
(即使你在Test类中添加一个静态成员,结果依然会提示因为该构造函数受其保护级别的限制而不可访问)

若是只在自己类里有私有类呢?

  public
   
  class
   ProgramTest
    {
        
  int
   j;
        
  private
   ProgramTest()
        {
            j 
  =
   
  3
  ;
            Console.WriteLine(
  "
  I am ProgramTest 默认构造函数,j={0}
  "
  , j);
        }
        
  static
   
  void
   Main(
  string
  [] args)
        {
  

              ProgramTest pt1 
  =
   
  new
   ProgramTest();
            Console.Read();
        }
    }

结果:I am ProgranmTest 默认构造函数,j=3

注意,如果您不对构造函数使用访问修饰符,则在默认情况下它仍为私有构造函数。但是,通常显式地使用private修饰符来清楚地表明该类不能被实例化。
若是一个类中既有实例构造函数又有私有构造函数,那么当实例对象是会怎么执行呢?

  public
   
  class
   Test
    {
        
  int
   i;
        
  private
    Test()
        {
            i 
  =
   
  1
  ;
            Console.WriteLine(
  "
  I am Test 默认构造函数 i={0}
  "
  , i);
        }
        
  public
   Test(
  int
   i)
        {
            Console.WriteLine(
  "
  I am Test 有参构造函数 i={0}
  "
  , i);
        }
    }
    
  public
   
  class
   ProgramTest
    {
        
  static
   
  void
   Main(
  string
  [] args)
        {
            Test t 
  =
   
  new
   Test();  
  //编译器提示:因为该构造函数受其保护级别的限制而不可访问        
  
            Console.Read();



若是只执行有参构造函数呢?


    
  public
   
  class
   Test
    {
        
  int
   i;
        
  private
    Test()
        {
            i 
  =
   
  1
  ;
            Console.WriteLine(
  "
  I am Test 默认构造函数 i={0}
  "
  , i);
        }
        
  public
   Test(
  int
   i)
        {
            Console.WriteLine(
  "
  I am Test 有参构造函数 i={0}
  "
  , i);
        }
    }
    
  public
   
  class
   ProgramTest
    {
        
  static
   
  void
   Main(
  string
  [] args)
        {
            Test t 
  =
   
  new
   Test(
  2
  );
            Console.Read();
        }
    }

结果:I am Test 有参构造函数 i=2

3.静态构造函数
静态构造函数用来初始化静态变量,这个构造函数是属于类的,而不是属于哪个实例的。

就是说这个构造函数只会被执行一次。也就是在创建第一个实例或引用任何静态成员之前,由.NET自动调用。

   public
    
   class
    Test
    {
        
   static
    
   int
    i;
        
   static
     Test()
        {
            i 
   =
    
   1
   ;
            Console.WriteLine(
   "
   I am Test 默认构造函数 i={0}
   "
   , i);
        }
    }
    
   public
    
   class
    ProgramTest
    {
        
   static
    
   void
    Main(
   string
   [] args)
        {
            Test t1 
   =
    
   new
    Test();
            Console.Read();
        }
    }

结果为:I am Test 默认构造函数 i=1

静态构造函数的特点:

1.静态构造函数既没有访问修饰符,也没有参数。

在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化类,也就是无法直接调用静态构造函数,也无法控制什么时候执行静态构造函数。

3.一个类只能有一个静态构造函数,最多只能运行一次。
4.静态构造函数不可以被继承。
5.如果没有静态构造函数,而类中的静态成员有初始值,那么编译器会自动生成默认的静态构造函数。
如果静态默认构造函数和公有默认构造函数同时存在,会怎么样呢?

      public
   
  class
   Test
    {
        
  static
   
  int
   i;
        
  static
    Test()
        {
            i 
  =
   
  1
  ;
            Console.WriteLine(
  "
  I am Test 静态默认构造函数 i={0}
  "
  , i);
        }
        
  public
   Test()
        {
            Console.WriteLine(
  "
  I am Test 公有默认构造函数 i={0}
  "
  , i);
        }
    }
    
  public
   
  class
   ProgramTest
    {
        
  static
   
  void
   Main(
  string
  [] args)
        {
            Test t1 
  =
   
  new
   Test();
            Console.Read();
        }
    }

结果:I am Test 静态默认构造函数 i=1

   


       I am Test 公有默认构造函数 i=1

如果静态默认构造函数和公有有参构造函数同时存在,我实例化类让它调用静态默认构造函数会怎么样呢?

   public
    
   class
    Test
    {
        
   static
    
   int
    i;
        
   static
     Test()
        {
            i 
   =
    
   1
   ;
            Console.WriteLine(
   "
   I am Test 静态默认构造函数 i={0}
   "
   , i);
        }
        
   public
    Test(
   int
    j)
        {
            Console.WriteLine(
   "
   I am Test 公有有参构造函数 i={0}
   "
   , j);
        }
    }
    
   public
    
   class
    ProgramTest
    {
        
   static
    
   void
    Main(
   string
   [] args)
        {
            Test t1 
   =
    
   new
    Test();
     
   //系统会提示错误       
   
            Console.Read();

  
          } 
      } 

如果静态默认构造函数和公有有参构造函数同时存在,我实例化类让它调用公有构造函数会怎么样呢?

       public
    
   class
    Test
    {
        
   static
    
   int
    i;
        
   static
     Test()
        {
            i 
   =
    
   1
   ;
            Console.WriteLine(
   "
   I am Test 静态默认构造函数 i={0}
   "
   , i);
        }
        
   public
    Test(
   int
    j)
        {
            Console.WriteLine(
   "
   I am Test 公有有参构造函数 i={0}
   "
   , j);
        }
    }
    
   public
    
   class
    ProgramTest
    {
        
   static
    
   void
    Main(
   string
   [] args)
        {
            Test t1 
   =
    
   new
    Test(
   2
   );
            Console.Read();
        }
    }

结果:I am Test 静态默认构造函数 i=1
I am Test 公有有参构造函数 j=2

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日霄科技

感谢各位大佬,您鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值