创建新实例或使用公共静态方法?
对于需要从多个页面调用方法的 ASP.NET 应用程序来说,哪一种是更好的方法?或者还有第三种更好的可能性吗?
Which one is the better way for a asp.net application in which i need to call methods from multiple pages? Or is there a third even better possibility?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两者都可以正常工作。另一种选择是为页面构建您自己的基类,并将通用功能放在那里。通过这种方法,您可以使方法成为虚拟的,并允许具体的类在适当的情况下覆盖功能。
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.
您还可以从公共基页继承所有页面。但如果您不需要访问 Page 类成员,我会选择公共静态方法。取决于你真正想要达到的目标
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. Depends on what you actually want to achieve