Java – how to view the class name that contains the actual code
•
Java
package a;
package a;
public class A {
public String toString() {
// return "I am an a.A"; is too primitive ;)
return "I am an " + getClass().getName(); // should give "a.A"
}
}
–
package a;
public class X extends A {
public static void main(String[] args) {
X test = new X();
System.out.println(test); // returns "I am an a.X"
}
}
I tried this, too GetClass () and super getClass(). How to get the class names of the actual codes tostring() and getclass()? (A.A)
This is just a simplified example. My point is how to avoid hard coding the base class name in the first file (a.java)
Solution
Change:
getClass().getName()
reach
A.class.getName()
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
