sing System;
using System.Collections.Generic;
using System.Text;namespace ConstructorDemo
{
    class student
    {
        public string ID;
        public string Name;
        public int Age;
        public Student()
        {
            ID = "空";
            Name = "空";
            Age = "0";
        }
        public Student(string id, string name, int age)
        {
            ID = id;
            Name = name;
            Age = age;
        }
        public void ShowStudent()
        {
            Console.WriteLine("学号:{0},姓名:{1},年龄:{2}", ID, Name, Age);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student stu1 = new Student();
            stu1.ShowStudent();
            Student stu2 = new Student("090304121","万文渊",20);
            stu2.ShowStudent();        }
    }
}
     错误提示:错误 1 类、结构或接口方法必须有返回类型 F:\PROGRAM\C#\Error Program of C#\ConstructorDemo\ConstructorDemo\Program.cs 12 16 ConstructorDemo
错误 2 类、结构或接口方法必须有返回类型 F:\PROGRAM\C#\Error Program of C#\ConstructorDemo\ConstructorDemo\Program.cs 18 16 ConstructorDemo

解决方案 »

  1.   


    sing System;
    using System.Collections.Generic;
    using System.Text;namespace ConstructorDemo
    {
      class student
      {
      public string ID;
      public string Name;
      public int Age;//Age定义成int型
      public Student()//这个我想你是构造函数,既然构造函数就要跟你的类名一样,也就是Student改成小写
      {
      ID = "空";
      Name = "空";
      Age = "0";//你却给他赋值字符串
      }
      public Student(string id, string name, int age)
      {
      ID = id;
      Name = name;
      Age = age;
      }
      public void ShowStudent()
      {
      Console.WriteLine("学号:{0},姓名:{1},年龄:{2}", ID, Name, Age);
      }
      }
      class Program
      {
      static void Main(string[] args)
      {
      Student stu1 = new Student();
      stu1.ShowStudent();
      Student stu2 = new Student("090304121","万文渊",20);
      stu2.ShowStudent();  }
      }
    }