创建实用程序类?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您应该将其设为静态类,如下所示:
该类(通常)不应具有任何字段或属性。 (除非您想在代码中共享某个对象的单个实例,在这种情况下,您可以创建一个
static
只读属性。You should make it a
static
class, like this: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.如果您使用 .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, likeInt32
, 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
您最好使用带有
static
方法的static
类。然后您将不需要实例化您的实用程序类来使用它。它看起来像这样:然后你可以像这样使用它:
You will be better off using a
static
class withstatic
methods. Then you won't need to instantiate your utilities class to use it. It will look something like this:Then you can use it like this:
最好的方法是使函数对类的成员不可靠。因此,您可以将函数设为静态。
更好的方法是使函数成为类型的扩展方法。 请参见此处的示例
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
虽然 OOP 新手并试图掌握最佳实践,但尝试避免实用程序类可能是个好主意。您可以像这样重新设计您的类
And 被称为:
这将是一个很好的实践,直到进一步的 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
And is called like:
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.
是的,这不会编译,因为静态类内部不支持 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
调用:-
int result = Utilities.sum(1, 2);
Method Calling :-
int result = Utilities.sum(1, 2);
做这个。
这样你就可以像这样使用它
do this.
That way you use it like