📜  Java中的 StringWriter getClass() 方法及示例

📅  最后修改于: 2022-05-13 01:54:21.067000             🧑  作者: Mango

Java中的 StringWriter getClass() 方法及示例

Java中StringWriter类的getClass()方法用来获取这个StringWriter实例的父类。此方法不接受任何参数并返回所需的类详细信息。

句法:

参数:此方法接受不接受任何参数。

返回值:此方法返回作为 StringWriter 实例的父类的类详细信息

下面的方法说明了 getClass() 方法的工作:

方案一:

// Java program to demonstrate
// StringWriter getClass() method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a StringWriter instance
            StringWriter writer
                = new StringWriter();
  
            // Get the String
            // to be written in the stream
            String string = "GeeksForGeeks";
  
            // Write the string
            // to this writer using write() method
            writer.write(string);
  
            // Get Class details using getClass()
            System.out.println("Parent Class: "
                               + writer.getClass());
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
Parent Class: class java.io.StringWriter

方案二:

// Java program to demonstrate
// StringWriter getClass() method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a StringWriter instance
            StringWriter writer
                = new StringWriter();
  
            // Get the String
            // to be written in the stream
            String string = "GFG";
  
            // Write the string
            // to this writer using write() method
            writer.write(string);
  
            // Get Class details using getClass()
            System.out.println("Parent Class: "
                               + writer.getClass());
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
Parent Class: class java.io.StringWriter