简体   繁体   English

创建新实例还是使用公共静态方法?

[英]create new instances or use public static methods?

Which one is the better way for a asp.net application in which i need to call methods from multiple pages? 对于需要从多个页面调用方法的asp.net应用程序,哪种方法更好? Or is there a third even better possibility? 还是有第三种甚至更好的可能性?

Either works just fine. 两种都很好。 Another alternative is to build your own base class for pages, and put the common functionality there. 另一种选择是为页面构建您自己的基类,并在其中放置通用功能。 With that approach, you can make the methods virtual and allow concrete classes to override functionality where appropriate. 使用这种方法,您可以使方法成为虚拟方法,并允许具体的类在适当的地方覆盖功能。

You can also inherit all the pages from a common base page. 您还可以从通用基础页面继承所有页面。 But I would opt for the public static method if you do not need access to Page class members. 但是,如果您不需要访问Page类成员,那么我将选择public static方法。 Depends on what you actually want to achieve 取决于您实际想要实现的目标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM