你为什么就不能调用从静态方法非静态方法? [英] Why can't you call a non-static method from a static method?

查看:208
本文介绍了你为什么就不能调用从静态方法非静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的课,要求在同一类的另一种方法[方法],而不是一个静态方法静态方法[方法1]。但是,这是一个没有没有。我得到这个错误:

I have a static method [Method1] in my class, which calls another method [Method2] in the same class and is not a static method. But this is a no-no. I get this error:

这是对象引用需要
非静态字段,方法或
财产ClassName.MethodName()

An object reference is required for the non-static field, method, or property "ClassName.MethodName()"

可有人请简要介绍一下为什么? 。包括可能与此相关的其他东西。

Can someone please briefly describe why? including other things that might be related to this.

编辑:!
感谢您的答复,伙计

Thanks for the responses, guys!

这是我的一个小故障,让我试着解释。类是工厂的一部分,工厂拥有所有类的实例。但这种方法是无处可寻,因为它不是由类实现的接口多数民众赞成的一部分,相反,它只是添加为辅助函数。

This was a small fault on my part, let me try and explain. The class was a part of a factory, and the factory has instance of all the classes. But this method was NoWhere to be found because it wasn't part of the interface thats implemented by the class, instead it was just added as an helper function.

答案也非常有帮助!

推荐答案

一个非静态方法需要的实例类的的。除非你在一个实例已经过去了,或者在你的方法创建一个实例,你不能调用非静态方法,因为你不知道这个方法应该在操作类的东西实例。

A non-static method requires an instance of the class. Unless you have passed in an instance, or created an instance in your method, you cannot call a non-static method, as you have no idea what instance of the class that method should operate on.

这篇关于你为什么就不能调用从静态方法非静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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