创建一个实用程序类? [英] Creating An Utilities Class?
问题描述
我很新的OOP,我试图我最难的事情保持严格的基础类,同时采用良好的编码原则。
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?
推荐答案
您应该让一个静态
类,像这样的:
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.
这篇关于创建一个实用程序类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!