📜  Java中的期望AssertionStatus() 方法与示例

📅  最后修改于: 2022-05-13 01:54:38.686000             🧑  作者: Mango

Java中的期望AssertionStatus() 方法与示例

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

句法:

public boolean desiredAssertionStatus()

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

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

下面的程序演示了所需的AssertionStatus() 方法。

示例 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–