本帖最后由 黒■色 于 2014-4-1 14:45 编辑
namespace 构造函数
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
Person p2 = new Person("tom");
Person p3 = new Person("jerry",18);
Console.WriteLine("年龄是{0},名字叫{1}",p1.Age,p1.Name);
Console.WriteLine("年龄是{0},名字叫{1}", p2.Age, p2.Name);
Console.WriteLine("年龄是{0},名字叫{1}", p3.Age, p3.Name);
Console.ReadKey();
}
}
class Person
{
public string Name { get;set; }
public int Age { get; set; }
public Person()
{
Name = "未命名";
Age = 0;
}
public Person(string name)
{
this.Name = name;
}
public Person(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
}
namespace 重载
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Add(9, 9));
Console.WriteLine(Add(0.1, 0.3));
Console.WriteLine(Add(111111111111111, 777777777777777));
Console.ReadLine();
}
static int Add(int a,int b)
{
return a + b;
}
static double Add(double a, double b)
{
return a + b;
}
static long Add(long a, long b)
{
return a + b;
}
}
}
重载和构造函数同样都是根据参数来选择方法,那他们有什么区别呢?
|