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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。