简体   繁体   English

如何从子类的静态方法获取类?

[英]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.

相关问题 如何从另一个类的静态方法获取抽象类的子类的实例? - How can I get an instance of a subclass of abstract class from a static method of another class? 我如何在超类方法中使用子类的静态字段 - How do I use static field of subclass in super class method Java:如何使用超类的静态方法从其创建子类的实例 - Java: How to Create an instance of a subclass from with a Static method of the super class 如何从一个方法,另一个类的变量数据中读取公共静态变量并获取更新的数据 - How do I read in a public static, with variable data from a method, from another class and get updated data 通过子类实例调用时,如何在子类的方法中使用子类的静态字段? - How do I use a subclass' static fields in superclass' method when calling via instance of subclass? 如何在非静态方法中从另一个类调用非静态方法? (java) - How do I call a non-static method from another class in a non-static method? (java) 如何从Java中的超类方法获取子类值? - How can I get subclass value from super class method in Java? 在另一个函数中创建子类的实例时,为什么不能从子类类型的 Abstact 类调用方法(公共或静态)? - Why can't I call a method (public or static) from an Abstact class of type subclass when creating an instance of the subclass in another function? 如何让子类使用父类的受保护方法? - How do I get a subclass to use a parent class' protected methods? 如何从一个方法更改静态类变量的值,然后从另一个方法获取它呢? - How can I change the value of a static class variable from a method, then get it from an other method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM