为什么静态方法只允许调用静态方法而不是非静态方法
可能的重复:
为什么我只能从静态访问静态成员功能?
当我尝试从静态方法内部调用普通方法时,出现错误:
非静态字段、方法或属性需要对象引用
因此这意味着我需要创建该类的对象,然后调用非静态方法。如果我想直接调用该方法,那么我必须将该方法声明为静态。
但是,在这种情况下,调用方法和被调用方法属于同一个类。那么为什么我需要在从静态方法调用时创建一个对象,而我可以从非静态方法调用非静态方法。
例如:
class Program
{
//public void outTestMethod(int x,out int y)
//{
// y = x;
//}
static void Main(string[] args)
{
int a = 10;
int b = 100;
outTestMethod(a,out b);
}
private void outTestMethod(int x, out int y)
{
y = x;
}
}
错误:非静态字段、方法或属性需要对象引用
Possible Duplicate:
Why can I only access static members from a static function?
While I was trying to call a normal method from inside a static method I got the error:
An object reference is required for the non-static field, method, or property
So this means I need to create the object of the Class and then call the nonstatic method. If I want to call the method directly then I have to declare that method as Static.
But , in this scenario , the calling method and called method belong to same class. So Why do I need to create an object while calling from a Static Method , while I can call a non-static method from non static method.
Ex:
class Program
{
//public void outTestMethod(int x,out int y)
//{
// y = x;
//}
static void Main(string[] args)
{
int a = 10;
int b = 100;
outTestMethod(a,out b);
}
private void outTestMethod(int x, out int y)
{
y = x;
}
}
Error:An object reference is required for the non-static field, method, or property
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
静态方法可以调用实例方法 - 但您需要有一个实例来调用它们。该实例具体来自哪里并不重要,例如:
实例方法与类型的特定实例相关联,而静态方法与整个类型相关联 - 对于其他类型的成员也是如此。因此,要调用实例方法,您需要知道您对哪个实例感兴趣。例如,拥有:
因为您需要知道哪个人是 没有意义的你在谈论:
……这更有意义。
通常实例方法使用或修改实例的状态。
Static methods can call instance methods - but you need to have an instance on which to call them. It doesn't matter where that instance comes from particularly, so for example:
Instance methods are associated with a particular instance of the type, whereas static methods are associated with the overall type instead - and the same is true for other kinds of members. So to call an instance method, you need to know which instance you're interested in. For example, it would be meaningless to have:
because you need to know which person you're talking about:
... that makes much more sense.
Typically instance methods use or modify the state of the instance.
这样考虑吧。
静态方法是一组电梯外面的按钮。任何人都可以看到它并推动它,并使某些事情发生(即其中一部电梯将到达该楼层)。
非静态方法是特定电梯内的按钮。他们操纵那部电梯(而不是其他电梯)。
Consider it this way.
A static method is the button outside a bank of elevators. Anyone can see it and push it, and make something happen (i.e. one of the elevators will arrive at that floor).
The non-static methods are the buttons inside a specific elevator. They manipulate THAT elevator (and none of the others).
非静态方法也称为实例方法。这意味着(通常)有一块数据,特定于该方法所操作的实例(对象)。
您不能从静态方法调用非静态或实例方法,因为它不知道要操作哪个实例或对象。
A non-static method is also called an instance method. This means there (usually) is a chunk of data, specific to the instance (object) that the method operates on.
You can't call a non static or instance method from a static method because it wouldn't know which instance or object to operate on.
因为没有实例可以调用该方法。您应该创建另一个类并对其进行测试:
Because there is no instance to call the method from. YOu should create possibly another class and test on that:
你明白实例方法和静态方法的区别吗?
实例方法可以访问
this
对象,即使它没有作为参数传入,事实上,它就像是框架为您传递的类的相同类型的不可见参数。静态方法没有
this
对象,并且无法调用实例方法,因为它没有任何内容以不可见的方式传递给该this
...听起来像个笑话但这就是我的看法:)
do you understand the difference between instance method and static method?
an instance method has access to the
this
object even if it's not passed in as parameter, in fact it's like if it was an invisible parameter of the same type of the class passed for you by the framework.a static method does not have that
this
object and cannot call instance methods because it does not have anything to pass for thatthis
in the invisible way...sounds like a joke but this is the way I see it :)