Java 我可以在不实例化此类的情况下使用类的方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2394205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use methods of a class without instantiating this class?
提问by Roman
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
回答by μBio
If the methods are static, yes.
如果方法是静态的,是的。
But you won't be able to access non-static members.
但是您将无法访问非静态成员。
回答by Tony The Lion
That would be static methods.
那将是静态方法。
回答by Yoni
回答by user272879
In most languages you can do it only if method is static. And static methods can change only static variables.
在大多数语言中,只有在方法是静态的情况下才能这样做。而静态方法只能改变静态变量。
回答by Boris Pavlovi?
Since qre is a static method and doesn't have an access to instances of the enclosing class you'll have first to create an instance and then access it. For example:
由于 qre 是一个静态方法并且无法访问封闭类的实例,因此您必须首先创建一个实例,然后再访问它。例如:
public class Foo {
private int bar;
public static void qre() {
Foo foo = new Foo();
foo.bar = 5;
System.out.println("next bar: " + (++5));
}
}
回答by Mark Byers
I have a class with several methods and there is no constructor among these methods.
我有一个包含多种方法的类,并且这些方法中没有构造函数。
If you don't explicitly define a constructor then you get a default constructor provided by the compiler. So if those methods aren't static, try this:
如果您没有明确定义构造函数,那么您将获得编译器提供的默认构造函数。所以如果这些方法不是静态的,试试这个:
NameOfClass x = new NameOfClass();
x.doMethod(x1,x2,...,xn);
回答by pmr
As many have pointed out: This is only possible if the method is static. Maybe some OOP background is in order: A method should always belong to a class. So what is the use of calling a method without an instance of a class? In a perfect OO world there shouldn't be any reason to do that. A lot of use cases that have to do with static methods talk about assigning some kind of identity to your class. While this is perfectly reasonable in a programming world it isn't very convincing when it comes to object oriented design.
正如许多人指出的那样:这只有在方法是静态的情况下才有可能。也许一些 OOP 背景是合适的:一个方法应该总是属于一个类。那么在没有类的实例的情况下调用方法有什么用呢?在完美的 OO 世界中,不应该有任何理由这样做。许多与静态方法有关的用例都在讨论为您的类分配某种身份。虽然这在编程世界中是完全合理的,但在面向对象的设计中并不是很有说服力。
As we program in an imperfect world there is often a use case for a "free function" (The way Java or C++ implement sort() for example). As Java has no direct support for free functions classes with only static "methods" are used to express those semantics with the added benefit of the class wrapper providing a "namespace". What you think of this workaround and if you see it as a flaw in language design is IMO a matter of opinion.
当我们在一个不完美的世界中编程时,通常会有一个“自由函数”的用例(例如 Java 或 C++ 实现 sort() 的方式)。由于 Java 不直接支持自由函数,因此类仅使用静态“方法”来表达这些语义,并具有提供“命名空间”的类包装器的额外好处。您对这种解决方法的看法以及您是否认为它是语言设计中的缺陷,这是 IMO 的意见问题。
回答by Richard Ozenbaugh
In proper encapsulation, you should not "see" what is happening upon instanciation. To rely on a class's lack of a constructor is breaking this form. The designed of the class my have in mind to add formal state initialization in the constructor at a later date. Your "contract" with the class is only that you can use the methods as they are currently designed.
在适当的封装中,您不应该“看到”实例化时发生的事情。依赖类缺少构造函数正在破坏这种形式。该类的设计我想在以后在构造函数中添加正式的状态初始化。您与该类的“合同”只是您可以使用当前设计的方法。
If you desire to use the functionality of that method without the class overhead, maybe it best for you to include that method in your existing "client" class (of course this is just "copy and paste" coding and is considered an anti-pattern of of software design.
如果您希望在没有类开销的情况下使用该方法的功能,那么最好将该方法包含在您现有的“客户端”类中(当然这只是“复制和粘贴”编码,被认为是一种反模式的软件设计。
回答by TofuBeer
I have a class with several methods and there is no constructor among these methods.
我有一个包含多种方法的类,并且这些方法中没有构造函数。
Do you mean you have something like:
你的意思是你有这样的东西:
public class X
{
public void foo()
{
}
}
or do you mean you have something like:
或者你的意思是你有类似的东西:
public class X
{
private X()
{
}
public void foo()
{
}
}
If it is the fist way then, yes, there is a constructor and it will look like this:
如果是第一种方式,是的,有一个构造函数,它看起来像这样:
public X()
{
super();
}
if it is the second way then there is probably a method like:
如果是第二种方式,那么可能有一种方法,例如:
public static X createInstance()
{
return (new X());
}
If you really mean can classes have methods that do things without ever creating an instance, then yes you can, just make all of the methods and variables static (usually this is not a good idea, but for some things it is perfect).
如果您真的是说类可以具有无需创建实例就可以执行操作的方法,那么是的,您可以,只需将所有方法和变量设为静态(通常这不是一个好主意,但对于某些事情来说它是完美的)。
回答by janm
A method on a class operates in the context an instance; it has access to the instance's member variables. You understand this, because you ask about what happens if the method accesses one of those variables.
类上的方法在实例的上下文中操作;它可以访问实例的成员变量。您理解这一点,因为您询问如果该方法访问这些变量之一会发生什么。
You can understand why it doesn't work by asking yourself the question: "Where is the data?" If you don't have an instance, where is the instance variable? Where is the data? And the answer is that it doesn't have a place and therefore doesn't work.
你可以通过问自己这个问题来理解为什么它不起作用:“数据在哪里?” 如果您没有实例,那么实例变量在哪里?数据在哪里?答案是它没有位置,因此不起作用。
The difference with a static function and static member variables is that you can answer the question about the location of the data. The static variables available regardless of whether there is a specific instance or not. The instance specific vs. class specific decision is one that you must make considering what you actually want to do.
静态函数和静态成员变量的区别在于您可以回答有关数据位置的问题。无论是否有特定实例,静态变量都可用。实例特定与类特定的决定是您必须考虑到您实际想要做什么的决定。