java field.gettype,Java Field getType()用法及代码示例

java.lang.reflect.Field的getType()方法用于获取此Field对象表示的字段的声明类型。此方法返回一个Class对象,该对象标识声明的类型

用法:

public String getType()

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

返回值:此方法返回一个Class对象,该对象标识声明的类型。

以下示例程序旨在说明getType()方法:

示例1:

// Java program to demonstrate getType() method

import java.lang.reflect.Field;

public class GFG {

public static void main(String[] args)

throws Exception

{

// Get the marks field object

Field field = User.class.getField("Marks");

// Apply getType Method on User Object

// to get the Type of Marks field

Class value = field.getType();

// print result

System.out.println("Type"

+ " is " + value);

// Now Get the Fees field object

field = User.class.getField("Fees");

// Apply getType Method on User Object

// to get the Type of Fees field

value = field.getType();

// print result

System.out.println("Type"

+ " is " + value);

}

}

// sample User class

class User {

// static double values

public static double Marks = 34.13;

public static float Fees = 3413.99f;

public static double getMarks()

{

return Marks;

}

public static void setMarks(double marks)

{

Marks = marks;

}

public static float getFees()

{

return Fees;

}

public static void setFees(float fees)

{

Fees = fees;

}

}

输出:

Type is double

Type is float

示例2:

// Java program to demonstrate getType() method

import java.lang.reflect.Field;

import java.time.Month;

public class GFG {

public static void main(String[] args)

throws Exception

{

// Get all field objects of Month class

Field[] fields = Month.class.getFields();

for (int i = 0; i < fields.length; i++) {

// print name of Fields

System.out.println("Name of Field: "

+ fields[i].getType());

}

}

}

输出:

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

Name of Field: class java.time.Month

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值