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


Java SimpleScriptContext removeAttribute()用法及代码示例


SimpleScriptContext类的removeAttribute()方法用于删除给定范围中的属性,并且属性和范围的名称作为参数传递。

用法:

public Object removeAttribute(String name, int scope)

参数:此方法接受两个参数:


  • name这是属性的名称,
  • scope这是删除属性的范围。

返回值:此方法返回删除的值。

异常:此方法引发以下异常:

  • NullPointerException :如果名称为null。
  • IllegalArgumentException:如果名称为空或范围无效。

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

// Java program to demonstrate 
// SimpleScriptContext.removeAttribute() 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); 
  
        // remove using removeAttribute() 
        Object removedObject 
            = simple.removeAttribute( 
                "name", 
                ScriptContext.ENGINE_SCOPE); 
  
        // print 
        System.out.println("Removed Object:"
                           + removedObject); 
    } 
}
输出:
Removed Object:Value

程序2:

// Java program to demonstrate 
// SimpleScriptContext.removeAttribute() 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.ENGINE_SCOPE); 
        simple.setAttribute("Team3", "Nepal", 
                            ScriptContext.GLOBAL_SCOPE); 
  
        // remove using removeAttribute() 
        Object remove1 
            = simple.removeAttribute( 
                "Team1", 
                ScriptContext.ENGINE_SCOPE); 
        Object remove2 
            = simple.removeAttribute( 
                "Team2", 
                ScriptContext.ENGINE_SCOPE); 
  
        // print scopes of different teams 
        System.out.println("Removed:" + remove1); 
        System.out.println("Removed:" + remove2); 
    } 
}
输出:
Removed:India
Removed:Japan

参考:https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleScriptContext.html#removeAttribute(java.lang.String,int)



相关用法


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