构造函数链接

发布于 2024-12-07 08:01:52 字数 306 浏览 2 评论 0原文

我有以下代码:

    public MapReader(string fName) {
        FileName = fName;
    }

    public MapReader(){
        Console.WriteLine("Input valid file name:");
        string name = Console.ReadLine();
        this(name);
    }

显然这是 Java 方法,在 C# 中不起作用。是否有任何选项不需要添加初始化方法?

I have following code:

    public MapReader(string fName) {
        FileName = fName;
    }

    public MapReader(){
        Console.WriteLine("Input valid file name:");
        string name = Console.ReadLine();
        this(name);
    }

Apparently this is Java approach, which is not working in C#. Is there any option which doesn't need added method for initialization?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

追星践月 2024-12-14 08:01:52

您无法在 C# 中执行此操作。您必须在另一个构造函数中设置该属性。

理想情况下,您应该分离出对控制台的依赖关系。

You can't do this in C#. You will have to set the property in the other constructor.

Ideally, you should separate out the dependency on the console.

花间憩 2024-12-14 08:01:52

在 C# 中你不能使用这种方法。
试试这个:

private void setParam(string name) {
    FileName = name;
}

public MapReader(string fName) {
    setParam(fName);
}

public MapReader() {
    Console.WriteLine("Input valid file name:");
    string name = Console.ReadLine();
    setParam(name);
}

In C# you couldn' use that approach.
Try this:

private void setParam(string name) {
    FileName = name;
}

public MapReader(string fName) {
    setParam(fName);
}

public MapReader() {
    Console.WriteLine("Input valid file name:");
    string name = Console.ReadLine();
    setParam(name);
}
最佳男配角 2024-12-14 08:01:52

尽管我同意其他人的观点,即对控制台的依赖对于此类来说可能不是最好的,但这是可行的:

class MapReader
{
    public string FileName { get; private set; }
    public MapReader(string fName)
    {
        FileName = fName;
    }

    public MapReader() : this(ObtainNameFromConsole())
    {

    }

    private static string ObtainNameFromConsole()
    {
        Console.WriteLine("Input valid file name:");
        return Console.ReadLine();
    }
}

Although I agree with others that having a dependency on the Console is probably not best for this class, this would work:

class MapReader
{
    public string FileName { get; private set; }
    public MapReader(string fName)
    {
        FileName = fName;
    }

    public MapReader() : this(ObtainNameFromConsole())
    {

    }

    private static string ObtainNameFromConsole()
    {
        Console.WriteLine("Input valid file name:");
        return Console.ReadLine();
    }
}
痴情换悲伤 2024-12-14 08:01:52

也许是这样的?

public MapReader(string fName)
{
    FileName = fName;
}

public static MapReader FromConsole()
{
    Console.WriteLine("Input valid file name:");
    string name = Console.ReadLine();
    return new MapReader(name);
}

Maybe something like this?

public MapReader(string fName)
{
    FileName = fName;
}

public static MapReader FromConsole()
{
    Console.WriteLine("Input valid file name:");
    string name = Console.ReadLine();
    return new MapReader(name);
}
-小熊_ 2024-12-14 08:01:52

您可以这样做:

public MapReader(string fName) {
   if (fName == null)
   {
     Console.WriteLine("Input valid file name:");
     fName = Console.ReadLine();
    }
    FileName = fName;
}

public MapReader() : this (null) {}

You can to do so:

public MapReader(string fName) {
   if (fName == null)
   {
     Console.WriteLine("Input valid file name:");
     fName = Console.ReadLine();
    }
    FileName = fName;
}

public MapReader() : this (null) {}
楠木可依 2024-12-14 08:01:52

我不太喜欢这样的副作用构造函数的方法,你可以像这样模拟同样的事情:

public class MapReader
{
    private string fileName;

    private MapReader(Func<string> fileName)
    {
        this.fileName = fileName();
    }

    public MapReader(string fileName) : this(() => fileName)
    {
    }

    public MapReader() : this(() => 
        {
            Console.WriteLine("Input valid file name:");
            return Console.ReadLine();
        })
    {
    }
}

I don't quite like the approach of having a side effecting constructor like that, you could simulate the same thing like this:

public class MapReader
{
    private string fileName;

    private MapReader(Func<string> fileName)
    {
        this.fileName = fileName();
    }

    public MapReader(string fileName) : this(() => fileName)
    {
    }

    public MapReader() : this(() => 
        {
            Console.WriteLine("Input valid file name:");
            return Console.ReadLine();
        })
    {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文