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
相關用法
- Java SimpleScriptContext setBindings()用法及代碼示例
- Java SimpleScriptContext getAttribute()用法及代碼示例
- Java SimpleScriptContext removeAttribute()用法及代碼示例
- Java SimpleScriptContext setAttribute()用法及代碼示例
- Java SimpleScriptContext getBindings()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 SimpleScriptContext getAttributesScope() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。