Java Writer getClass()方法及示例

Java Writer getClass()方法及示例

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

语法

public final Class String getClass()

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

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

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

程序 1 :

// 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 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

程序2

// 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 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程