Java:返回类(不是实例)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1932625/
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
Java: Return class (Not an instance)
提问by Martijn Courteaux
Is it possible to return in a static method a class? I will explain...
是否可以在静态方法中返回一个类?我会解释...
I have:
我有:
public class A { public static void blah(){} }
public class B { }
I want to create a static method in B witch returns A
. So you can do:
我想在 B 女巫返回中创建一个静态方法A
。所以你可以这样做:
A.blah();
And
和
B.getA().blah();
This, without creating an instance of A
. Just use it static methods.
这样,无需创建A
. 只需使用它静态方法。
Is this possible?
这可能吗?
采纳答案by Stephen C
This is a rebuttal of @irreputable's answer:
这是对@irreputable 回答的反驳:
public class B {
public static A getA(){ return null; }
}
B.getA().blah(); //works!
It "works", but probably not in the sense that you expect, and certainly not in a useful way. Let's break this down into two parts:
它“有效”,但可能不是您所期望的,当然也不是有用的方式。让我们把它分成两部分:
A a = B.getA();
a.blah();
The first statement is returning a (null in this case) instance of A
, and the second statement is ignoring that instance and calling A.blah()
. So, these statements are actually equivalent to
第一条语句返回一个(在本例中为 null) 的实例A
,第二条语句忽略该实例并调用A.blah()
. 所以,这些语句实际上等价于
B.getA();
A.blah();
or (given that getA()
is side-effect free), just plain
或(鉴于getA()
没有副作用),只是简单的
A.blah();
And here's an example which illustrates this more clearly:
这是一个更清楚地说明这一点的例子:
public class A {
public static void blah() { System.err.println("I'm an A"); }
}
public class SubA extends A {
public static void blah() { System.err.println("I'm a SubA"); }
}
public class B {
public static A getA(){ return new SubA(); }
}
B.getA().blah(); //prints "I'm an A".
... and this (I hope) illustrates why this approach doesn't solve the OP's problem.
...这(我希望)说明了为什么这种方法不能解决 OP 的问题。
回答by Confusion
No, this is not possible. You can only return a reference to an instance of a class. The closest you can get to this is to return a reference to a variable of type Class. The question is: why do you want this? What problem are you trying to solve? There may be a better solution.
不,这是不可能的。您只能返回对类实例的引用。最接近的方法是返回对 Class 类型变量的引用。问题是:你为什么想要这个?你想解决什么问题?可能有更好的解决方案。
回答by tangens
Even if it would be possible, it won't be of much use. The call A.blah()
doesn't create an instance of A
. It's a static method with no need for an instance.
即使有可能,也没有多大用处。该调用A.blah()
不会创建A
. 这是一个不需要实例的静态方法。
And you can't use interfaces to implement static methods. So what should it be good for?
并且您不能使用接口来实现静态方法。那么它应该有什么用呢?
回答by zedoo
No this is not possible. You have two options:
不,这是不可能的。您有两个选择:
B.getA()
returns an instance of A, andblah()
will be a non-static method.Directly call
A.blah()
.
B.getA()
返回 A 的一个实例,并且blah()
将是一个非静态方法。直接调用
A.blah()
。
回答by Bozho
If you don't want to have an instance of A
, then let B
call blah()
directly. I.e.
如果您不想拥有 的实例A
,则直接B
调用blah()
。IE
class B {
void blah() {
A.blah();
}
}
回答by cyborg
I'm going to guess that the reason you ask this is that you want B to return many different classes with different behaviours - not just A.
我猜你问这个的原因是你希望 B 返回许多具有不同行为的不同类 - 而不仅仅是 A。
You probably want to use an interface for what you're doing instead.
您可能希望使用接口来代替您正在做的事情。
interface IA {
void blah();
}
public class B {
IA getA1() {
return new IA {
void blah() {
...code...
}
}
}
IA getA2() {
...
}
IA getA3() {
...
}
}
myCallingMethod {
B.getA1().blah();
B.getA2().blah();
B.getA3().blah();
}
回答by Peter Lawrey
You can return a Method using reflection which you can invoke() later. However, it sounds like you are trying to do something which should be done another way. Why are you trying to do this?
您可以使用反射返回一个方法,稍后您可以调用该方法。但是,听起来您正在尝试做一些应该以另一种方式完成的事情。你为什么要这样做?
回答by Chad Okere
People are saying it's impossible, and that's kind oftrue, but if you use the reflection API you can do something close to that.
人们说这是不可能的,这是真的,但是如果您使用反射 API,您可以做一些接近于此的事情。
Here's how you could do it.
这是你可以做到的。
You have a class that does this.
你有一个这样做的类。
public class B {
Class a
public static Class getA(){
return a;
}
}
then to call blah you do:
然后打电话给你做:
try{
Method m = B.getA().getDeclaredMethod("blah");
m.invoke(null);//for a static method, you can invoke on null
}
Catch(Exception e){
// see documentation for list of exceptions
}
So, why would you want to do this? Well, if you do it this way you can change the class A at So getA() could return A, B, C or D, all with different blah() functions. I'm not really sure what purpose that would serve, but if you want to do it, you can.
那么,你为什么要这样做呢?好吧,如果你这样做,你可以在类 A 处更改 getA() 可以返回 A、B、C 或 D,所有这些都具有不同的 blah() 函数。我不确定这会起到什么作用,但如果你想这样做,你可以。
see: Class.getDeclaredMethod()and Method.invoke()for more info.
请参阅: Class.getDeclaredMethod()和Method.invoke()了解更多信息。
I haven't tried this, so you might need to do some tweaking.
我没有试过这个,所以你可能需要做一些调整。
回答by irreputable
public class B {
public static A getA(){ return null; }
}
B.getA().blah(); //works!
EDIT
编辑
It is true that it is equivalent to
确实,它等价于
B.getA();
A.blah();
Except they lookquite different. Imagine you have a chain of these.
除了它们看起来完全不同。想象一下,你有一连串的这些。
I checked out org.apache.commons.cli.OptionBuilder
and personly I wouldn't do it that way, but the author has his case. The API is used only in the beginning of a program, in a single thread.
我检查了org.apache.commons.cli.OptionBuilder
,我个人不会那样做,但作者有他的情况。API 仅在程序开始时在单个线程中使用。
API designers sometimes have to make some magic moves, don't be too judgemental.
API 设计者有时不得不做出一些神奇的举动,不要太主观。