创建实用程序类?

发布于 2024-09-01 19:52:31 字数 387 浏览 6 评论 0原文

我对 OOP 很陌生,并且正在尽最大努力使事情严格基于类,同时使用良好的编码原则。

我现在已经进入我的项目了,我有很多通用方法想要放入实用程序类中。有没有创建实用程序类的最佳方法?

public class Utilities
{
    int test;

    public Utilities()
    {
    }

    public int sum(int number1, int number2)
    {
        test = number1 + number2;
    }
    return test;
}

创建此 Utilities 类后,我是否只创建一个 Utilities 对象并运行我选择的方法?我的实用程序类想法正确吗?

I'm very new to OOP and am trying my hardest to keep things strictly class based, while using good coding principles.

I'm a fair ways into my project now and I have a lot of general use methods I want to put into an utilities class. Is there a best way to create a utilities class?

public class Utilities
{
    int test;

    public Utilities()
    {
    }

    public int sum(int number1, int number2)
    {
        test = number1 + number2;
    }
    return test;
}

After creating this Utilities class, do I just create an Utilities object, and run the methods of my choosing? Do I have this Utilities class idea correct?

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

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

发布评论

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

评论(8

意中人 2024-09-08 19:52:31

您应该将其设为静态类,如下所示:

public static class Utilities {
    public static int Sum(int number1, int number2) {
        return number1 + number2;
    }
}

int three = Utilities.Sum(1, 2);

该类(通常)不应具有任何字段或属性。 (除非您想在代码中共享某个对象的单个实例,在这种情况下,您可以创建一个 static 只读属性。

You should make it a static class, like this:

public static class Utilities {
    public static int Sum(int number1, int number2) {
        return number1 + number2;
    }
}

int three = Utilities.Sum(1, 2);

The class should (usually) not have any fields or properties. (Unless you want to share a single instance of some object across your code, in which case you can make a static read-only property.

開玄 2024-09-08 19:52:31

如果您使用 .NET 3.0 或更高版本,则应该研究扩展方法。它们允许您编写一个静态函数,该函数将针对特定类型(例如Int32)进行操作,同时看起来是该对象的方法。那么你就可以得到:int result = 1.Add(2);

试试这个;它可能只是向您展示另一种方式。 ;)

C# 教程 -扩展方法

If you are working with .NET 3.0 or above, you should look into extension methods. They allow you to write a static function that will act against a particular type, like Int32, while seeming to be a method on that object. So then you could have: int result = 1.Add(2);.

Try this out; it might just show you another way. ;)

C# Tutorial - Extension Methods

依 靠 2024-09-08 19:52:31

您最好使用带有 static 方法的 static 类。然后您将不需要实例化您的实用程序类来使用它。它看起来像这样:

public static Utilites
{
  public static int sum(int number1, int number2)
  {
     test = number1+number2;
     return test;
  }
}

然后你可以像这样使用它:

int result = Utilites.sum(1, 3);

You will be better off using a static class with static methods. Then you won't need to instantiate your utilities class to use it. It will look something like this:

public static Utilites
{
  public static int sum(int number1, int number2)
  {
     test = number1+number2;
     return test;
  }
}

Then you can use it like this:

int result = Utilites.sum(1, 3);
岛歌少女 2024-09-08 19:52:31

最好的方法是使函数对类的成员不可靠。因此,您可以将函数设为静态。

更好的方法是使函数成为类型的扩展方法。 请参见此处的示例

Best is to make the functions not reliable to members of a class. Therefore you can make the functions static.

Better is to make the functions an extension method of a type. see here for example

長街聽風 2024-09-08 19:52:31

虽然 OOP 新手并试图掌握最佳实践,但尝试避免实用程序类可能是个好主意。您可以像这样重新设计您的类

public class Sum
{
    private int _result;

    public int Result {
       get {
           return _result;
       }
    }

    public Sum(int startNum) {
        _results = startNum;
    }

    public void Add(int num) {
        _result += num;
    }
}

And 被称为:

Sum sum = new Sum(1);
sum.add(2);
int result = sum.Result;

这将是一个很好的实践,直到进一步的 OOP 经验可以帮助您检查使用实用程序类与纯 OOP 原则的权衡。

While new to OOP and trying to get a handle on best practices, it may be a good idea to try to avoid utility classes. You could redesign your class like

public class Sum
{
    private int _result;

    public int Result {
       get {
           return _result;
       }
    }

    public Sum(int startNum) {
        _results = startNum;
    }

    public void Add(int num) {
        _result += num;
    }
}

And is called like:

Sum sum = new Sum(1);
sum.add(2);
int result = sum.Result;

It'll be good practice until further experience with OOP can help you examine the trade-offs of using an utility class vs pure OOP principles.

断桥再见 2024-09-08 19:52:31

是的,这不会编译,因为静态类内部不支持 int test ,或者将其设置为受支持的 static int test 并返回输出

yes this does not compile because int test which is not supported inside static class either make it as static int test which will be supported and returns the output

琉璃梦幻 2024-09-08 19:52:31
  1. 创建实用程序类作为公共静态类。
  2. 使用 static 关键字定义实用程序函数方法方法

调用:-

int result = Utilities.sum(1, 2);

   public static class MyMath
         {
             int result;

             public static int sum(int number1, int number2)
             {
                 result= number1+number2;
                 return result;
             }
        }
  1. Create Utility class as public static class.
  2. Defined Utility function-method with static keyword

Method Calling :-

int result = Utilities.sum(1, 2);

   public static class MyMath
         {
             int result;

             public static int sum(int number1, int number2)
             {
                 result= number1+number2;
                 return result;
             }
        }
彩扇题诗 2024-09-08 19:52:31

做这个。

public static class Utilities
{
    int test;

    public static int sum(int number1, int number2)
    {
        test = number1+number2;
        return test;
    }
}

这样你就可以像这样使用它

int result = Utilities.sum(1, 2);

do this.

public static class Utilities
{
    int test;

    public static int sum(int number1, int number2)
    {
        test = number1+number2;
        return test;
    }
}

That way you use it like

int result = Utilities.sum(1, 2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文