这个 JAVA朗,反思 方法isDefault() 方法用于检查method对象的方法是否为 默认方法: 或者不是。如果method对象是默认方法,则返回true,否则将返回false。
null
默认方法: 静态方法上的公共非抽象,其主体在接口类型中声明。
语法:
public boolean isDefault()
返回值: 此方法返回一个 布尔值 价值它回来了 符合事实的 如果方法对象是JVM规范中的默认方法,则为 错误的 .
下面的程序演示了method类的isDefault()方法:
例1:
/* * Program Demonstrate isDefault() method * of Method Class. */ import java.lang.reflect.Method; public class GFG { // create main method public static void main(String args[]) { try { // create class object for interface Shape Class c = Shape. class ; // get list of Method object Method[] methods = c.getMethods(); // print Default Methods for (Method m : methods) { // check whether the method is Default Method or not if (m.isDefault()) // Print System.out.println( "Method: " + m.getName() + " is Default Method" ); } } catch (Exception e) { e.printStackTrace(); } } private interface Shape { default int draw() { return 0 ; } void paint(); } } |
输出:
Method: draw is Default Method
例2:
/* * Program Demonstrate isDefault() method * of Method Class. * This program checks all default method in Comparator interface */ import java.lang.reflect.Method; import java.util.Comparator; public class Main6 { // create main method public static void main(String args[]) { try { // create class object for Interface Comparator Class c = Comparator. class ; // get list of Method object Method[] methods = c.getMethods(); System.out.println( "Default Methods of Comparator Interface" ); for (Method method : methods) { // check whether method is Default Method or not if (method.isDefault()) // print Method name System.out.println(method.getName()); } } catch (Exception e) { e.printStackTrace(); } } } |
输出:
Default Methods of Comparator Interface reversed thenComparing thenComparing thenComparing thenComparingInt thenComparingLong thenComparingDouble
参考: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#isDefault–
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END