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”个参数的构造函数
求解~~!

解决方案 »

  1.   


    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("你好");
            }
        }
      

  2.   

    using System;
    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!!");
            }
        }}
      

  3.   

    中国人 : Person因为"中国人"这个类有一个默认的0参构造函数,这就要求它的基类也有一个0参的构造函数,但是不存在,所以报错了。你只需要给Person添加一个无参的构造函数就行了。 
      

  4.   

    类中国人继承类person
    中国人 p2 = new 中国人();//必须写入参数
      

  5.   


    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("你好");
            }
        }
      

  6.   

    这个 到没注意到 我百度了下 得到结果因为构造子类前必定构造父类 中国人继承自Person 而当你(new 中国人)的时候 首先会先构造Person 而你的父类Person又没有0个参数的构造函数 所以当然出错. 
      

  7.   

        class 中国人 : Person
        {
            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!!");
            }
        }
      

  8.   

    像Person p1 = new Person("ahgnsan", 20);一样传入加上参数就可以了
      

  9.   

    base 是什么东西 还么的见过、、、、
      

  10.   

    http://msdn.microsoft.com/zh-cn/library/hfw7t1ce%28v=VS.80%29.aspx
    http://msdn.microsoft.com/zh-cn/library/ms173115%28v=VS.80%29.aspx如果基类没有提供默认构造函数,派生类必须使用 base 显式调用基构造函数。
      

  11.   

    继承的类,初始化的时候会先执行基类的构造函数,如你的“中国人”类,继承了Person类;则初始化“中国人”类的时候,会先执行基类Person的构造函数。Person基类里面有两个构造函数的重载,两个构造函数都有参数,但你的继承类“中国人”里构造函数是没有给基类的构造函数提供参数。以致报错。
    有三个办法:
    一、给基类提供无参数的构造函数:
    在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!!");
       }
    }
      

  12.   

    类里面 如果构造函数,系统自动生成一个无参数的构造函数,有构造函数之后系统就提供无参数的构造函数了,“中国人”这个类继承了Person类,在初始化 “中国人”这个类的时候 会先去调用person的构造函数,由于没有指定调用哪个构造函数,默认调用无参数的构造函数。
      

  13.   

    楼主,看修改后的代码,有注释原因:using System;
    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();
            }
        }
    }
      

  14.   

     //如果想调用基类中带有参数的构造函数,可以再派生类中添加一个这样的构造函数:
                public China(string name,string thukou)
                    : base(name)//这里指明了要调用基类的哪一个构造函数,避免调用积累的默认构造函数
                {
                    hukou = thukou;
                }