Creating a Utilities Class?

2019-03-23 02:32发布

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?

7条回答
Bombasti
2楼-- · 2019-03-23 03:22

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);
查看更多
登录 后发表回答