📜  Java中的编写器 getClass() 方法和示例

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

Java中的编写器 getClass() 方法和示例

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

句法:

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

返回值:此方法返回类详细信息,它是 Writer 实例的父类。

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

方案一:

// Java program to demonstrate
// Writer getClass() method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a Writer instance
            Writer writer
                = new PrintWriter(System.out);
  
            // Get the String
            // to be written in the stream
            String string = "GeeksForGeeks";
  
            // Write the 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.PrintWriter

方案二:

// Java program to demonstrate
// Writer getClass() method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a Writer instance
            Writer writer
                = new OutputStreamWriter(System.out);
  
            // Get the String
            // to be written in the stream
            String string = "GFG";
  
            // Write the 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.OutputStreamWriter