當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java SimpleScriptContext getAttributesScope()用法及代碼示例


SimpleScriptContext類的getAttributesScope()方法用於返回定義屬性並將屬性名稱作為參數傳遞的範圍。

用法:

public int getAttributesScope(String name)

參數:此方法接受單個參數名稱,即屬性名稱。


返回值:如果沒有在任何範圍內定義具有給定名稱的屬性,則此方法返回最低範圍,並返回-1。

異常:此方法引發以下異常:

  • NullPointerException 如果名稱為null。
  • IllegalArgumentException如果名稱為空。

以下示例程序旨在說明SimpleScriptContext.getAttributesScope()方法:
程序1:

// Java program to demonstrate 
// SimpleScriptContext.getAttributesScope() method 
  
import javax.script.ScriptContext; 
import javax.script.SimpleScriptContext; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create SimpleScriptContext object 
        SimpleScriptContext simple 
            = new SimpleScriptContext(); 
  
        // add some attribute 
        simple.setAttribute( 
            "name", 
            "Value", 
            ScriptContext.ENGINE_SCOPE); 
  
        // get scope using getAttributesScope() 
        int scope = simple.getAttributesScope("name"); 
  
        // print 
        System.out.println("Scope:" + scope); 
    } 
}
輸出:
Scope:100

程序2:

// Java program to demonstrate 
// SimpleScriptContext.getAttributesScope() method 
  
import javax.script.ScriptContext; 
import javax.script.SimpleScriptContext; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create SimpleScriptContext object 
        SimpleScriptContext simple 
            = new SimpleScriptContext(); 
  
        // add some attribute 
        simple.setAttribute( 
            "Team1", 
            "India", 
            ScriptContext.ENGINE_SCOPE); 
        simple.setAttribute( 
            "Team2", 
            "Japan", 
            ScriptContext.GLOBAL_SCOPE); 
        simple.setAttribute( 
            "Team3", 
            "Nepal", 
            ScriptContext.GLOBAL_SCOPE); 
  
        // get scope using getAttributesScope() 
        int scope1 
            = simple.getAttributesScope("Team1"); 
        int scope2 
            = simple.getAttributesScope("Team2"); 
        int scope3 
            = simple.getAttributesScope("Team3"); 
  
        // print scopes of different teams 
        System.out.println("Scope for Team1:"
                           + scope1); 
        System.out.println("Scope for Team2:"
                           + scope2); 
        System.out.println("Scope for Team3:"
                           + scope3); 
    } 
}
輸出:
Scope for Team1:100
Scope for Team2:-1
Scope for Team3:-1

參考:https://docs.oracle.com/javase/10/docs/api/javax/script/ScriptContext.html



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 SimpleScriptContext getAttributesScope() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。