繁体   English   中英

控制台应用程序 (C#) 不从不同的文件重新注册类

[英]console app (C#) doesnt regonize classes from different file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ex1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            createCity("ex", 1, "ex1");
            Console.ReadKey();
        }

        public void createCity(string name, int showOrder, string objectName)
        {
            City myObj = new City(name, showOrder);
        }
    }
}


    using System;
    
    public class City
{
    public City()
    {
        string name;
        static int code = 1;
        int showOrder;

        City(string name, int showOrder)
        {
            this.name = name;
            this.showOrder = showOrder;
            this.code++;
        }
    }
}

知道为什么吗? 新加入 C#。
它不知道为什么class这个城市,我不明白为什么。 我在同一个项目中使用添加选项创建了它。 我只是想在 main 中创建一个新的 class 的 object。

  • 创建这样的构造函数:

    public City() { }

或带有参数:

public City(string name, int showOrder)
      {
          ...
      }
  • 在构造函数之外定义 class 的属性或字段

public class City { private string name { get; set; } private int code { get; set; } = 1; private int showOrder { get; set; } }

  • 您的“代码”属性/字段不应为 static。这会导致您的构造函数出错

    int code = 1;

  • 您不能在 main 方法中执行 createCity() 的原因是因为它被标记为 static。删除 static 关键字应该可以。

    private void Main(string[] args) { createCity("ex", 1, "ex1"); 控制台.ReadKey(); }

但是,您需要在控制台应用程序中使用 static void Main(string[] args) 方法,所以我也创建了 createCity static

完整的工作示例:

    namespace ex1
{
    public class Program
    {
        private static void Main(string[] args)
        {
            createCity("ex", 1, "ex1");
            Console.ReadKey();
        }

        public static void createCity(string name, int showOrder, string objectName)
        {
            City myObj = new City(name, showOrder);
        }
    }
}

public class City
{
    public City()
    {
    }

    private string name { get; set; }
    private int code { get; set; } = 1;
    private int showOrder { get; set; }

    public City(string name, int showOrder)
    {
        this.name = name;
        this.showOrder = showOrder;
        this.code++;
    }
}

有很多事情要解决:

static :请确保您理解这个概念这适用于:

  • static void createCity
  • code++;

City class 有多个构造函数,并且 class 变量定义在错误的地方。 您必须将构造函数设置为public或至少设置为internal
public City(string name, int showOrder)

更正后的代码:

程序.cs:

using System;

namespace ex1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            createCity("ex", 1, "ex1");
            Console.ReadKey();
        }

        public static void createCity(string name, int showOrder, string objectName)
        {
            City myObj = new City(name, showOrder);
        }
    }
}

城市.cs:

namespace ex1
{
    public class City
    {
        string name;
        static int code = 1;
        int showOrder;

        public City()
        {
        }

        public City(string name, int showOrder)
        {
            this.name = name;
            this.showOrder = showOrder;
            code++;
        }
    }
}

请尝试以下代码:

using System;

namespace ex1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            City c = createCity("ex", 1, "ex1");
            Console.WriteLine(c.ToString());
            Console.ReadLine();
        }

        public static City createCity(string name, int showOrder, string objectName)
        {
            return new City(name, showOrder);
        }
    }

    public class City
    {
        private string name;
        private int code = 1;
        private int showOrder;

        public City(string name, int showOrder)
        {
            this.name = name;
            this.showOrder = showOrder;
            this.code++;
        }

        public override string ToString() => $"name: {name}; code:{code}; showOrder: {showOrder}";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM