简体   繁体   English

一个类如何访问自己的类名?

[英]How can a class access its own classname?

I use an java application which generates a class dynamically. 我使用一个动态生成类的java应用程序。 Via an ant script the source code will be produced for a give classname and a class template. 通过ant脚本,将为给定的类名和类模板生成源代码。

In the template for the class I need to know the name of even this class, to call a static method of the class. 在类的模板中,我需要知道甚至这个类的名称,来调用类的静态方法。

Example. 例。 The class will be named "VersionInfo". 该类将命名为“VersionInfo”。 Then in static main() of it I want to call the static method: VersionInfo.getId(). 然后在静态main()中我想调用静态方法:VersionInfo.getId()。 But I don't know the class-name. 但我不知道班级名称。

Is there an equivalent to "this" for static contexts or some Utility-Class for such a purpose? 对于静态上下文或某些实用程序类是否有相当于“this”的目的?

如果您是通过Ant创建类,那么为什么不生成一个返回类名的静态方法getClassName?

如果你的main方法驻留在同一个类中,你可以在main方法中调用getId()

So you're saying that it should generate this? 所以你说它应该生成这个?

public class VersionInfo{ // VersionInfo class name changes, per problem description
    public static void main(){
        System.out.println(getId()); 
// but in the main within the class,we don't need the classname to call a static method
    }
    public static string getId(){
       return "what's the problem?";
    }
}

Is there something missing from the description, that you're calling some OTHER class' static method by an unknown-to-the-template name? 在描述中是否缺少某些东西,你是通过一个未知的模板名称调用一些OTHER类的静态方法?

There's a nasty workaround: 有一个令人讨厌的解决方法:

public static final Class THIS_CLASS = new Object() {
  public Class getParentClass() {
    return getClass().getEnclosingClass();
  }
}.getParentClass();

I'm not sure I understand. 我不确定我理解。 If you generate the class VersionInfo yourself, why can't you get the class name from the code that generates the class? 如果您自己生成类VersionInfo ,为什么不能从生成类的代码中获取类名?

Try this: 尝试这个:

package uk.co.farwell.stack_overflow;

public class Test_847708 {
    private final static String getId() {
        return "string";
    }

    public static void main(String args[]) {
        System.out.println("getId=" + getId());
    }

}

You cannot use the key "this" in a static context. 您不能在静态上下文中使用键“this”。

Instead, if you want to call dynamically a static function, you can use java reflection . 相反,如果要动态调用静态函数,可以使用java反射

I cannot help you further for java reflection because I never use it, but I already use it in .Net and it's a powerful tools. 我无法帮助你进一步了解java反射,因为我从不使用它,但我已经在.Net中使用它,它是一个强大的工具。

The cleanest answer to the question might be to make a third class with a static, known, name that is generated by the ANT script which references the dynamic class name, and then have your main method reference that known class. 对问题最干净的答案可能是使用由引用动态类名称的ANT脚本生成的静态已知名称创建第三个类,然后让主方法引用该已知类。

If for some reason that isn't enough, then combine Joachim Sauer and Melursus answer, and get the class name, and then get the method via reflection: 如果由于某种原因还不够,那就结合Joachim Sauer和Melursus的答案,得到类名,然后通过反射得到方法:

Method m = THIS_CLASS.getDeclaredMethod("getId", null);
Object result = m.invoke(null, null);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非法访问错误:类<classname>无法访问其超级接口<interfacename> - IllegalAccessError:class <classname> cannot access its superinterface <interfacename> 如何在自己的类中初始化对象? - How can an object be initialized within its own class? 如何序列化一个可以作为其自身类型参数的泛型 class? - How to serialize a generic class that can be its own type parameter? 类无法使用来自不同包的反射来访问其自己的受保护的成员变量 - Class can't access its own protected member variable using reflection from different package 为什么一个类的实例可以访问它自己类型的另一个实例的私有字段? - Why can an instance of a class access private fields of another instance of its own type? 如果线程只能访问具有不同线程创建的具有自己实例的类的静态方法,则该线程将在哪个线程上执行? - If threads only can access static methods to a class with its own instance, created by a different thread, what thread will this execute on? 一个类可以有自己的专用PrintWriter吗? - Can a class have its own private PrintWriter? 即使没有受保护,类如何访问其超类变量? - How can a class access its super class variables even there are not protected? 如何检测Java类是由其自己的main()调用还是从另一个类调用? - How can I detect if a Java class is called by its own main() or from another class? 如何用自己的类型的成员扩展类? - How to extend a class with a member of its own type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM