Java ObjectStreamField getType()方法与示例
ObjectStreamField类getType()
方法
getType()方法在java.io包中可用。
getType()方法用于获取此ObjectStreamField字段的类型。
getType()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
getType()方法在返回类型时不会引发异常。
语法:
public Class getType();
参数:
它不接受任何参数。
返回值:
方法的返回类型为Class,它返回返回字段类型的Class对象,否则,当类型为非原始类型并且此ObjectStreamField由反序列化的ObjectStreamClass字段形成时,返回对象Class。
示例
//Java程序演示示例 //类getType()方法 //的ObjectStreamField- import java.io.*; import java.util.*; public class GetTypeOfOSF { public static void main(String[] args) { //实例化Calendar的ObjectStreamClass- ObjectStreamClass o_sc = ObjectStreamClass.lookup(Calendar.class); //通过使用getField()方法是获取字段 //日历中的值 ObjectStreamField field1 = o_sc.getField("isTimeSet"); ObjectStreamField field2 = o_sc.getField("fields"); //通过使用getType()方法就是返回 //字段的类型 Class field1_type = field1.getType(); System.out.println("field1.getType(): " + field1_type); Class field2_type = field2.getType(); System.out.println("field2.getType(): " + field2_type); } }
输出结果
field1.getType(): boolean field2.getType(): class [I