How does Java recognize type information at run time?

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Author {
<span class="hljs-keyword">private String pen_name;
<span class="hljs-keyword">private String real_name;
}
输出 com.cmower.java_demo.fifteen.Author
    }
}

wanger. Getclass() gets the class object of Wanger through C1 Getname() gets the class name of the Wanger object.

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Author {
<span class="hljs-keyword">private String pen_name;
<span class="hljs-keyword">private String real_name;
}

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Writer <span class="hljs-keyword">extends <span class="hljs-title">Author {
<span class="hljs-keyword">private String honour;
}

<span class="hljs-keyword">public <span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Test {
<span class="hljs-function"><span class="hljs-keyword">public <span class="hljs-keyword">static <span class="hljs-keyword">void <span class="hljs-title">main <span class="hljs-params">(String [] args) {
Author wanger = <span class="hljs-keyword">new Writer();
Class c1 = wanger.getClass();
System.out.println(c1.getName());
<span class="hljs-comment">//输出 com.cmower.java_demo.fifteen.Writer
}
}

<span class="hljs-keyword">try {
Class c3 = Class.forName(<span class="hljs-string">"com.cmower.java_demo.fifteen.Writer");
System.out.println(c3.getName());
} <span class="hljs-keyword">catch (ClassNotFoundException e) {
e.printStackTrace();
}

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
分享
二维码
< <上一篇
下一篇>>