当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Class desiredAssertionStatus()用法及代码示例


java.lang.Class类的desiredAssertionStatus()方法用于获取此类的声明状态,该状态将在调用此方法时分配给该类。该方法以布尔值的形式返回此类的断言状态。

用法:

public boolean desiredAssertionStatus()

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


返回值:此方法以布尔值的形式返回此类的指定断言状态。

下面的程序演示了desiredAssertionStatus()方法。

范例1:

// Java program to demonstrate 
// desiredAssertionStatus() method 
  
import java.util.*; 
  
public class Test { 
  
    public Object obj; 
  
    public static void main(String[] args) 
        throws ClassNotFoundException 
    { 
  
        // returns the Class object for this class 
        Class myClass = Class.forName("Test"); 
  
        System.out.println("Class represented by myClass:"
                           + myClass.toString()); 
  
        // Get the Assertion Status of myClass 
        // using desiredAssertionStatus() method 
        System.out.println("Assertion Status of myClass:"
                           + myClass.desiredAssertionStatus()); 
    } 
}
输出:
Class represented by myClass:class Test
Assertion Status of myClass:false

范例2:

// Java program to demonstrate 
// desiredAssertionStatus() method 
  
import java.util.*; 
  
class Main { 
  
    private Object obj; 
  
    public static void main(String[] args) 
        throws ClassNotFoundException, NoSuchFieldException 
    { 
  
        try { 
  
            // returns the Class object for this class 
            Class myClass = Class.forName("Main"); 
  
            System.out.println("Class represented by myClass:"
                               + myClass.toString()); 
  
            // Get the Assertion Status of myClass 
            // using desiredAssertionStatus() method 
            System.out.println("Assertion Status of myClass:"
                               + myClass.desiredAssertionStatus()); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
输出:
Class represented by myClass:class Main
Assertion Status of myClass:false

参考: https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#desiredAssertionStatus-



相关用法


注:本文由纯净天空筛选整理自srinam大神的英文原创作品 Class desiredAssertionStatus() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。