什么是理想的情况下(现实生活中的例子)来创建,只是要创建帮手静态方法? [英] What are the desirable situation (real life example) to create static methods except for creating helper?

查看:188
本文介绍了什么是理想的情况下(现实生活中的例子)来创建,只是要创建帮手静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解的目的是静态方法提供,什么是理想的情况下,我可以创造静态方法,除了有些人会说是用于创建帮手静态方法。

I just want to understand the purpose that static method serves and what are the desirable situation where i can create static methods except some would say that static methods are used for creating helper.

考虑我有1网站,将在我公司只喜欢人力资源管理系统喜欢的网站中使用。

Consider i have 1 website that will be used in my company only like Human resource management system like websites.

现在后管理登录到系统管理员会看到employees.so的方法是什么也不做多,从员工表中读取员工的所有细节,并显示他们在网站上简单的列表,此方法将在这样的业务接入层来定义这.NET:

Now after Admin login in to the system admin will see the list of employees.so the method is simple which does nothing more than fetching all details of employees from employee table and will display them on the web site and this method will be define in business access layer like this in .net:

 public class EmployeeBal
    {
        public List<Employee> GetAllEmployees()
        {
                 return Select * from Employee
        }
     }

这是我怎么会调用这个方法从我application.For EG(.aspx页面中或MVC控制器等....)

This is how i would call this method from my application.For Eg(.aspx page or mvc controller etc....)

var employeeBal= new EmployeeBal();
employeeBal.GetAllEmployees();



我的问题是我应该创建这个方法,静态方法和非静态方法??

So my question is should i create this method as static method or non static method??

请注意:这只是方法的一个例子,这种方法是在我的业务接入层

Note:This is just an example of method and this method is in my business access layer.

考虑我有1 电子商务网站,其中在主页上我正在展示的产品和该网站的访问一些列表中的每个用户可以看到的产品的清单。

Consider i have 1 ecommerce website where on the home page i am displaying some list of products and on visit of that website every users can see that list of products.

所以我的职能将是相同业务接取层之上定义:

so my function would be same as above define in Business acess layer:

public class ProductBal
    {
        public List<Product> DisplayProductonHomePage()
        {
                 return Select * from Products
        }
     }


$ {产品来自

返回SELECT *} b $ b

所以我的问题是一样的像是否要创建这个方法,静态方法和非静态方法,以及如果超过10个用户在同一时间同时访问这个网站,那么会有怎样的行为/影响将发生这种方法???

So my question would be same like whether to create this method as static method or non-static method and what will happen if more than 10 users at same time simultaneously access this website then what will be the behaviour/implications of this method???

请问这种方法将服务于这个每个用户的目的,如果我们声明此方法为静态的??

Will this method will serve the purpose of this each user if we declare this method as static??

谁能回答与简单说明每个方案???

Can anybody answer this question with briefly explaining every scenario???

推荐答案

一个静态方法是有道理的,当这个问题没有状态下保持。我是什么状态呢?那么,考虑以下因素:你有两个不同的对象, A B ,这两者都是类型的 EmployeeBal 。有曾经在你的程序的情况下 a.GetAllEmployees() b.GetAllEmployees()会产生不同的结果?

A static method makes sense when there’s no state to maintain. What do I mean by state? Well, consider the following: You have two distinct objects, a and b, which are both of type EmployeeBal. Is there ever a case in your program where a.GetAllEmployees() and b.GetAllEmployees() would yield different results?

如果没有,那么为什么对象 A b 存在呢?其对象的意义就在于一些明显的状态与他们相关联。如果两个不同的对象永远不能指不同的状态,那么他们履行没有意义。

If not, then why do the objects a and b exist at all? The whole point of having objects is to associate some distinct state with them. If two different objects can never refer to a different state, then they fulfil no purpose.

事实上,在这种情况下,你的 EmployeeBal 将完全等同于 System.Math ,其所有的方法都是辅助方法(如果这就是你想要打电话给他们什么)。在这种情况下,忘记了一分钟的静态方法:全班应该是静态的(静态类EmployeeBal ),它不应该有任何的构造函数;因为类型对象的概念 EmployeeBal 根本是没有意义的。事实上,在其他语言 EmployeeBal 不会是一类在所有;相反,它会是一般称为的模块的:一个单位进行逻辑分组的代码。 C#有没有模块,并且所有的代码必须驻留类中。由此类实现双重目的:他们组代码,它们产生的对象 1

In fact, in this situation your EmployeeBal would be exactly equivalent to System.Math, and all its methods are "helper methods" (if that’s what you want to call them). In this case, forget about static methods for a minute: your whole class should be static (static class EmployeeBal), and it should not have any constructors; because the concept of an object of type EmployeeBal simply makes no sense. In fact, in other languages EmployeeBal wouldn’t be a class at all; instead, it would be something generally called a module: a unit that logically groups code. C# has no modules, and all code must reside within classes. Classes thus fulfil a dual purpose: they group code, and they generate objects.1

现在考虑不那么极端的例子: EmployeeBal 实际上对象保持状态,并且差距。然而 GetAllEmployees()仍然会产生相同的结果,无论哪个对象调用的方法。

Now consider a less extreme case: EmployeeBal objects actually maintain state, and differ. Yet GetAllEmployees() will still yield the same result, regardless of which object calls the method.

在这种情况下, , EmployeeBal 显然不能是一个静态类。但 GetAllEmployees 仍是无状态的,因而不属于的对象的类型 EmployeeBal 。因此方法应该是静态的。

In this case, EmployeeBal obviously cannot be a static class. But GetAllEmployees is still stateless, and thus doesn’t belong to objects of type EmployeeBal. And thus the method should be static.

1 这种缺乏两种根本不同的区别概念(的模块的和的的)实际上是很烦人的,而C#的行为这种方式的主要原因是因为它被认为是类似于Java。这是在事后一个错误,但不严重。

1 This lack of distinction between two fundamentally distinct concepts (module and class) is actually quite annoying, and the main reason that C# behaves this way is because it was conceived to be similar to Java. It was a mistake in hindsight, but not a serious one.

这篇关于什么是理想的情况下(现实生活中的例子)来创建,只是要创建帮手静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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