明天补。
本博主患有严重拖延症
先看个有代表性的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static int FunctionTest(int x)
{
return x + 100;
}
static string FunctionTest(string str)
{
return str;
}
static int FunctionTest(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
Console.WriteLine(FunctionTest(10));
Console.WriteLine(FunctionTest("hello world!"));
Console.WriteLine(FunctionTest(5,20));
Console.ReadKey();
}
}
}
构造函数是和类名相同的类的一个方法,如果没有显式的声明,在系统会在编译的时候,自动生成一 个不带参数的,不执行任何动作的构造函数。
但如果显式的声明了构造函数,系统就不会自动生成了。如果声明的构造函数是有参数的构造函数, 我们在实例化类的时候,就必须以该构造函数而实例化类。看下面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
public int num;
public Program(int i)
{
this.num = i + 5;
}
static void Main(string[] args)
{
Program classOne = new Program(10);
int x = classOne.num;
Console.WriteLine(x);
Console.ReadKey();
}
}
}
如上代码,在实例化类的时候,test classOne=new test(10); 传递了一个参数。如果我们test classOne=new test();这样来实例化类,就会报错了。因为我们显式的声明了一个带参的构造方法,new test() 这样实例化的时候,调用的是无参的构造函数,但类中却没有无参的构造函数。
我们再来看一下静态构造函数。
在C# 中我们可以给类定义一个无参的静态构造函数(注意,必须是无参的),只要创建类的对象,该 方法就会执行。该函数只执行一次,并且在代码引用类之前执行。
一般,在类中有一些静态字段或者属性,需要在第一次使用类之前从外部数据源初始化这些静态字段 和属性,这时,我们就采用静态构造函数的方式来解决。静态构造函数没有访问修饰符,其他C#代码也不调用它,在加载类时,总是由.NET 运行库调用它。一 个类只能有一个静态构造函数。
注意,无参的实例构造函数可以和静态构造函数在类中共存。因为静态构造函数是在加载类的时候执 行的,而实例构造函数是在创建实例时执行的,两者并不冲突。
我们看下面的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class test
{
static test()
{
Console.WriteLine("静态构造类,在程序运行时被执行!");
}
public test()
{
}
static void Main(string[] args)
{
test trytest = new test();
Console.ReadKey();
}
}
}
在类的对象创建过程中,静态构造函数已经运行了。
我们再来看一个例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class test
{
private string domain;
private string url;
public test(string dom, string url)
{
this.domain = dom;
this.url = url;
}
public test(string dom)
{
this.domain = dom;
this.url = "www.baidu.com";
}
static void Main(string[] args)
{
test classTest = new test("baidu");
Console.WriteLine(classTest.url);
test classTwo = new test("www.sina.com.cn", "http://");
Console.WriteLine(classTwo.url + classTwo.domain);
Console.ReadKey();
}
}
}
在上例中,有两个构造函数,有可能两个构造函数需要初始化同一个字段,这种情况,C#中有个特殊 的语言,称为“构造函数初始化器”可以实现。
看下面代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication7
{
class test
{
private string domain;
private string url;
public test(string dom, string url)
{
this.domain = dom;
this.url = url;
}
public test(string dom)
: this(dom, "baidu.com")
{
}
static void Main(string[] args)
{
test classTest = new test("www.");
Console.WriteLine(classTest.domain + classTest.url);
Console.ReadKey();
}
}
}
如上实例,就是采用了 构造函数初始化器。注意,构造函数初始化器在构造函数之前执行。
2、只读字段(readonly)。
只读字段比常量灵活的多,常量(const)字段必须在声明之初就初始化,但readonly 字段甚至可以进行 一些运算再确定其值。
注意,可以在构造函数中对只读字段赋值,但不能在其他地方赋值。