我在类程序 1 中有一个返回类型错误,返回类型 " public Customer()" 错误


using System;
using System.Reflection;
namespace Reflection
{
    class Program
    {
        private static void Main(string[] args)
        {
            Type t = Type.GetType("program.program1");
            Console.WriteLine(t.FullName);
            PropertyInfo[] p = t.GetProperties();
            foreach (PropertyInfo pr in p)
            {
                Console.WriteLine(pr.Name);
            }
        }
    }
    public class Program1
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Customer(int ID, string Name)
        {
            this.Id = ID;
            this.Name = Name; 
        }
        public Customer()
        {
            this.Id = -1;
            this.Name = string.Empty;
        }
        public void PrintId()
        {
            Console.WriteLine(this.Id);
        }
        public void PrintName()
        {
            Console.WriteLine(this.Name);
        }
    }
}

构造函数的名字应该对应于类的名字,所以重命名

  public class Program1 {...

  // class "Customer"...
  public class Customer {
    public int Id { get; set; }
    public string Name { get; set; }
    // Constructor has the same name that the class it creates
    public Customer(int ID, string Name)
    {
       this.Id = ID;
       this.Name = Name; 
    }
    // Constructor "Customer" has the same name that the class it creates
    public Customer()
    {
        this.Id = -1;
        this.Name = string.Empty;
    }
    public void PrintId()
    {
        Console.WriteLine(this.Id);
    }
    public void PrintName()
    {
        Console.WriteLine(this.Name);
    }
  }

相关内容

最新更新