我可以在不实例化此类的情况下使用类的方法吗? [英] Can I use methods of a class without instantiating this class?

查看:19
本文介绍了我可以在不实例化此类的情况下使用类的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个方法的类,这些方法中没有构造函数.

所以,我想知道是否可以在不创建类的实例的情况下调用类的方法.

例如,我可以这样做:

NameOfClass.doMethod(x1,x2,...,xn)

总的来说,我不明白为什么它应该是不可能的.我只是调用一个函数来做某事(或返回一些值).如果可能,如果该方法为类的私有变量设置值会发生什么.我怎样才能达到这个值?一样吗?

NameOfClass.nameOfVariable

解决方案

它被称为静态变量和静态方法.试试看,看看它编译.<​​/p>

I have a class with several methods and there is no constructor among these methods.

So, I am wondering if it is possible to call a method of a class without a creation of an instance of the class.

For example, I can do something like that:

NameOfClass.doMethod(x1,x2,...,xn)

In general I do not see why it should be impossible. I just call a function which does something (or return some values). If it is possible, what will happen if the method sets a value for a private variable of the class. How can I reach this value? In the same way?

NameOfClass.nameOfVariable

解决方案

It's called static variables and static methods. Just try it and see that it compiles.

这篇关于我可以在不实例化此类的情况下使用类的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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