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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。