寻求帮助了解何时使用静态方法 [英] Seeking help understanding when to use static methods

查看:59
本文介绍了寻求帮助了解何时使用静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于使用静态方法的相互矛盾的意见。请考虑以下内容:



在我的大多数CRUD应用程序中,我使用SqlDataHandler类来处理与类(或某些情况下的类组)的数据库交互。请参阅以下内容:

I have read so many conflicting opinions on the use of static methods that my head hurts. Consider the following:

In most of my CRUD applications I use a SqlDataHandler class to handle the interactions with the database for a class (or in some cases group of classes). See below:

public abstract SqlDataHandler
{
    #region Properties
    protected static string ConnectionString { get; set; }
    #endregion

    #region Methods
    protected static DataTable GetDataTable(SqlCommand GetTableCommand)
    {
    	...
    }
    #endregion	
}

public AccountSqlDataHandler : SqlDataHandler
{
    #region Methods
    public static DataTable GetAccountHistory()
    {
    	SqlCommand getAccountHistoryCommand;
    	
    	...
    	
    	return AccountSqlDataHandler.GetDataTable(getAccountHistoryCommand);
    }
    #endregion
    
    #region Ctors
    static AccountSqlDataHandler()
    {
    	AccountSqlDataHandler.ConnectionString = "Connection string for account database";
    }
    #endregion
}

public Account
{
    #region Properties
    public List<HistoryItem> AccountHistory
    {
    	get
    	{
    		List<HistoryItem> accountHistory;
    		
    		accountHistory = this.getItemsFromDataTable(AccountSqlDataHandler.GetAccountHistory());
    		
    		return accountHistory;
    	}
    }
    #endregion
}



正如我所看到的那样,如果我使用成员方法,那么我每次都必须创建一个AccountSqlDataHandler实例,或者在Account类中创建一个AccountSqlDataHandler成员。我认为这样做没有任何好处,但从我正在阅读的内容中看,这是一个优势。在盲目改变我的方法之前,我想了解它是什么。


As I see it if I use member methods, then either I have to create an AccountSqlDataHandler instance each time, or create an AccountSqlDataHandler member in the Account class. I don't see any advantage to doing this, but from what I'm reading there is an advantage. I would like to understand what it is before I blindly change my methodology.

推荐答案

当我开始使用VB6时,我更喜欢模块而不是类,并且这有些类似,模块对应于静态。范式向班级的转变花了我一些时间。重要的一点是,我可以拥有一个不会互相干扰的类的多个实例:使用静态,整个应用程序中只存在一个项目 - 当你需要一个项目用于两个不同的目的时(在你的例子:你需要在你的应用程序中同时处理两个不同的帐户历史记录),然后你就会遇到麻烦。乍一看,这可能看起来不是一个很大的问题,但随着经验的增长,你将改变范式。我们可以尝试解释,但最终启蒙不能被别人带给你,它必须在你内部存在。
When I started working with VB6, I favored "modules" over "classes", and that's somehow similar, with modules corresponding to "static". The paradigm shift to classes took me some time. An important point was that I can have multiple instances of a class which do not interfere with each other: with "static", there exists only one single item in your whole application - and when you need that one item for two different purposes (in your example: you need to handle two different account histories in your application at the same time), then you'll get into trouble. At a first glance, that may not look like a really big issue, but with growing experience, you'll change the paradigm. We can try to explain, but in the end the enlightenment cannot be brought to you by others, it must come into existence within you.


这篇关于寻求帮助了解何时使用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆