using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 第二季
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person("ahgnsan", 20);
p1.SayHello();
中国人 p2 = new 中国人();
p2.Name = "lisi";
p2.gonfu();
p2.hukou = "nanjing";
Console.ReadKey();
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name)
{
this.Name = name;
}
public Person(int Age)
{
this.Age = Age;
}
public Person(string name, int age)
{
this.Age = age;
this.Name = name;
}
public void SayHello()
{
Console.WriteLine("你好");
}
}
class 中国人 : Person
{
public string hukou { set; get; }
public void gonfu()
{
Console.WriteLine("wo da!!");
}
}
}
错误 “第二季.Person”不包含采用“0”个参数的构造函数
求解~~!
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(){}//0个参数的构造函数
public Person(string name)
{
this.Name = name;
}
public Person(int Age)
{
this.Age = Age;
}
public Person(string name, int age)
{
this.Age = age;
this.Name = name;
}
public void SayHello()
{
Console.WriteLine("你好");
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person("ahgnsan", 20);
p1.SayHello();
中国人 p2 = new 中国人();
p2.Name = "lisi";
p2.gonfu();
p2.hukou = "nanjing";
Console.ReadKey();
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person()
{
}
public Person(string name)
{
this.Name = name;
}
public Person(int Age)
{
this.Age = Age;
}
public Person(string name, int age)
{
this.Age = age;
this.Name = name;
}
public void SayHello()
{
Console.WriteLine("你好");
}
}
class 中国人 : Person
{ public string hukou { set; get; }
public void gonfu()
{
Console.WriteLine("wo da!!");
}
}}
中国人 p2 = new 中国人();//必须写入参数
class Person
{
public string Name { get; set; }
public int Age { get; set; }
//这个类中要包含一个空参的构造函数
public Person()
{ }
//因为有带参数的构造函数,上面的空参函数是必须的
public Person(string name)
{
this.Name = name;
} public Person(int Age)
{
this.Age = Age;
}
public Person(string name, int age)
{
this.Age = age;
this.Name = name;
}
public void SayHello()
{
Console.WriteLine("你好");
}
}
{
public string hukou { set; get; } public 中国人(String name)
: base(name)
{
} public 中国人(Int32 age)
: base(age)
{
} public 中国人(Int32 age, String name)
: base(name, age)
{
} public void gonfu()
{
Console.WriteLine("wo da!!");
}
}
http://msdn.microsoft.com/zh-cn/library/ms173115%28v=VS.80%29.aspx如果基类没有提供默认构造函数,派生类必须使用 base 显式调用基构造函数。
有三个办法:
一、给基类提供无参数的构造函数:
在Person类里写
public Person(string name){ }
二、初始化继承类的构造函数提供参数:
中国人 p2 = new 中国人(20);或中国人 p2 = new 中国人("ahgnsan", 20);
三、在继承类的构造函数中提供基类的参数:
public class 中国人 : Person
{
public 中国人():base(30){}
public string hukou { set; get; }
public void gonfu()
{
Console.WriteLine("wo da!!");
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Solution
{
class Program
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; } public Person()
{//派生类在初始化对象的时候,会调用基类无参数的构造函数,所以基类必须带有一个默认构造函数(无参数的构造函数)
Console.WriteLine("==>调用了基类【Person】的无参数构造函数.");
}
public Person(string name)
{
this.Name = name;
Console.WriteLine("==>调用了基类【Person】的一个参数构造函数.");
}
public Person(int Age)
{
this.Age = Age;
}
public Person(string name, int age)
{
this.Age = age;
this.Name = name;
}
public void SayHello()
{
Console.WriteLine("你好");
}
}
public class China : Person
{//在派生类中无论调用派生类的那个构造函数,都会【先】调用基类的默认构造函数,来初始化从基类中继承的成员。
public China(string name)
{
Name = name;
Console.WriteLine("==>调用派生类【China】的一个参数构造函数.");
} public China()
{
Console.WriteLine("==>调用派生类【China】的无参数构造函数.");
} public string hukou { set; get; }
public void gonfu()
{
Console.WriteLine("wo da!!");
}
}
static void Main(string[] args)
{
Person p1 = new Person("ahgnsan", 20);
p1.SayHello(); Console.WriteLine("\n==>按任意键开始创建带[一个参数]的China对象:\n");
Console.ReadKey();
China c1 = new China("楼主"); //
//p2.Name = "lisi";
//p2.gonfu();
//p2.hukou = "nanjing"; Console.WriteLine("\n==>按任意键开始创建[无参数]的China对象:\n");
Console.ReadKey();
China c2 = new China(); Console.WriteLine("\n==>按任意键退出......\n");
Console.ReadKey();
}
}
}
public China(string name,string thukou)
: base(name)//这里指明了要调用基类的哪一个构造函数,避免调用积累的默认构造函数
{
hukou = thukou;
}