【问题标题】:Class, What's the best way to use methods?类,使用方法的最佳方式是什么?
【发布时间】:2011-05-26 11:38:33
【问题描述】:

我是编程新手,自学,昨天我正在开发一个使用 C# 处理文件的类,我有一个疑问......当你有一个 checkmethod 和一个 createmethod 时,使用这些方法的最佳方法是什么?

是的,我知道,我在这里不清楚,所以这里有一个例子;

Files.cs(类)

namespace Working_with_Files
{
  class Files
  {

    public bool CheckFile(string path)
    {
        if (File.Exists(path))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool CreateFile(string path)
    {
        if (CheckFile(path))
        {
            return false;
        }
        else
        {
            File.Create(path);
            return true;
        }
    }

  }
}

使用此类方法的最佳和最快方法是什么?因为当我使用 CreateFile 方法时,我必须检查是否已经存在同名的文件。

最好的方法是在这个方法中引用另一个方法?像这样;

namespace Working_with_Files
{
  class Files
  {

    public bool CheckFile(string path)
    {
        if (File.Exists(path))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool CreateFile(string path)
    {
        if (CheckFile(path))
        {
            return false;
        }
        else
        {
            File.Create(path);
            return true;
        }
    }

  }
}

最好的方法是在 CreateFile 方法中使用本机 File.Exists?像这样;

namespace Working_with_Files
{
  class Files
  {

    public bool CheckFile(string path)
    {
        if (File.Exists(path))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool CreateFile(string path)
    {
        if (File.Exists(path))
        {
            return false;
        }
        else
        {
            File.Create(path);
            return true;
        }
    }
  }
}

或者,最好和最快的方法是在使用 CreateFile 方法之前在主程序上使用 CheckFile 方法?

这是我的疑问,如果我不能说清楚,对不起。

【问题讨论】:

  • 你有理由包装 CheckFile,不是吗?
  • 首先,看起来它们应该是静态方法 - 我看不到 ctor 或实例变量...

标签: c# oop class methods


【解决方案1】:

就我个人而言,我是这样做的:

如果“检查”代码不止一行代码,那么我将其移至自己的方法中。

你也可以这样做:

return File.Exists(path);

在 CheckFile 方法中。

但是关于性能/速度,不用担心。写多少方法就写多少,速度差别那么小。

在我看来,代码的可读性比微小的性能更重要。

【讨论】:

    【解决方案2】:

    不要进行过早的优化!第一个是“更清楚”,这是一个主观问题。

    请重命名函数:如果函数名为 CheckFile,它应该“检查”文件、内容或其他内容。不检查文件是否存在 --> 重命名为 FileExists

    【讨论】:

      【解决方案3】:

      如果您想要最快的方式,那么我认为您可以在第一种情况下只使用您的 CreateFile 方法。因为它使用即用型框架 File.Exists 和 File.Create 方法。正如大多数开发人员所做的那样 - 如果框架或语言提供了即用型功能,则使用它们,否则如果不满足,则将最大存在的那些组合起来。

      希望对您有所帮助!

      【讨论】:

        【解决方案4】:

        假设你的方法需要额外的功能,而你并没有给百合镀金......

        我认为您是在问是否在另一种方法中复制一种方法的功能,答案是否定的。

        “在使用 CreateFile 方法之前在主程序上使用 CheckFile 方法”允许您扩展 CheckFile 方法,而不会使其功能与 CreateFile 不同,这是更好的封装。 (如果总是需要,也可以让 CreateFile 调用 CheckFile)

        【讨论】:

          【解决方案5】:

          无需创建 Files 类的实例,因此要么按照已经建议的方式将所有方法设为静态,要么使用我认为更优雅的代码模式:

          namespace Working_with_Files
          {
              public class Files
              {
                  private static Files instance;
                  public static Files Instance { get { return instance; } }
          
                  static Files()
                  {
                      instance = new Files();
                  }
          
                  private Files()
                  {
                  }
          
                  public bool CheckFile(string path)
                  ......no change in rest of code.....
              }
          }
          

          并调用方法:

          Files.Instance.CheckFile("myfilehere")
          

          【讨论】:

          • 这更多的是评论而不是答案,因为它与问题无关。更多一般提示。
          猜你喜欢
          • 2011-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-08
          • 1970-01-01
          • 2013-06-18
          • 1970-01-01
          相关资源
          最近更新 更多