Java.lang.Class.toString() 方法
描述
java.lang.Class.toString() 将对象转换为字符串。 字符串表示是字符串"class"或"interface",后跟一个空格,然后是由 getName 返回的格式的类的完全限定名称。
声明
以下是 java.lang.Class.toString() 方法的声明。
public String toString()
参数
NA
返回值
此方法返回此类对象的字符串表示形式。
异常
NA
示例
下面的例子展示了 java.lang.Class.toString() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class ClassDemo {
public static void main(String[] args) {
ClassDemo c = new ClassDemo();
Class cls = c.getClass();
// returns the string representation of this class object
String str = cls.toString();
System.out.println("Class = " + str);
// returns the name of the class
str = cls.getName();
System.out.println("Class = " + str);
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Class = class com.tutorialspoint.ClassDemo Class = com.tutorialspoint.ClassDemo

