我可以使用类的方法而不实例化这个类吗? [英] Can I use methods of a class without instantiating this class?

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

问题描述

我有一个有几个方法的类,这些方法中没有构造函数。

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天全站免登陆