[英]How do I get the class from a static method of a subclass?
/**
* this is my class, yadayada. Needs to be subclassed to be useful.
*/
public abstract class MyClass {
...
public static void myMethod() {
// What is the name of the subclass that's using this?
System.out.println("Called from class " + this.getClass().class.getName());
// Doesn't work because there's no "this" in a static method.
}
}
Edit: this is what I want to happen: 编辑:这是我想发生的事情:
public class FooClass extends MyClass {
...
}
FooClass.myMethod(); // I would like this to print "Called from class FooClass"
I'm all out of ideas. 我全都没主意了。
Edit: And for those who wonder why I'm doing it this way, it's actually an Android question of sorts. 编辑:对于那些想知道为什么我要这样做的人,这实际上是一个Android问题。
Android defines a class called BroadcastReceiver. Android定义了一个称为BroadcastReceiver的类。 You subclass this and register your subclass with the system. 您可以将此子类化并在系统中注册您的子类。 When an appropriate broadcast is sent, the system creates an instance of your subclass and calls one of its methods. 发送适当的广播后,系统将创建您的子类的实例并调用其方法之一。
I want to create my own BroadcastReceiver subclass that contains a static method for registering itself. 我想创建自己的BroadcastReceiver子类,其中包含用于注册自身的静态方法。 But I also want it to be subclassable. 但是我也希望它是可继承的。 It sort of looks like this: 看起来像这样:
/**
* My special-purpose BroadcastReceiver. Subclass this to use it.
*/
public class MyClass extends BroadcastReceiver {
public static scheduleAnAlarm(long when) {
// do some class-specific stuff
AlarmManager.scheduleBroadcast(MyClass.class, when);
}
public void onBroadcast() {
System.out.println("You should subclass MyClass for it to be useful");
}
}
My problem is that I want to register the subclass to be instantiated, not MyClass. 我的问题是我想注册要实例化的子类,而不是MyClass。
You can't. 你不能 That is you cannot get the class
of a subclass from a static
method. 那就是您不能从static
方法获得子class
的类。
When Java invokes a static
method on a class (even one that is abstract
) no subclass is required therefore none is loaded by the class loader. 当Java在类(甚至是abstract
类)上调用static
方法时,则不需要子类,因此类加载器不会加载任何子类。 Therefore there is not necessarily any subclass to get. 因此,不一定要获取任何子类。 Even if there was, there could be multiple subclasses so how could Java decide which one you want? 即使存在,也可能有多个子类,所以Java如何确定所需的子类?
Edit, even with your edit you can't get the subclass. 编辑,即使进行编辑也无法获得子类。 There is a trick you can use with exceptions to get the calling class. 您可以使用一个技巧与异常来获取调用类。 You can throw / catch an exception and then walk down the stack trace one level to get the calling method / class. 您可以抛出/捕获一个异常,然后在堆栈跟踪中向下移动一层以获得调用方法/类。 Not recommended! 不建议!
我想您想要类名,那么下面的方法可能会起作用:
System.out.println("Called from class " + MyClass.class.getName());
this
将无法在静态上下文中工作,您可以访问的类实例是MyClass.class
MyClass.class.getName()怎么样?
public class Foo extends MyClass{
public static void main(String[] args) {
myMethod();
}
}
and 和
public abstract class MyClass{
public static void myMethod() {
System.out.println("Called from class " + MyClass.class.getName());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.