1.package com.hongyuan.test;java
import java.util.List;ide
import javax.script.Invocable;函數
import javax.script.ScriptEngine;ui
import javax.script.ScriptEngineFactory;spa
import javax.script.ScriptEngineManager;代理
import javax.script.ScriptException;orm
public class ScriptTest {對象
public static void main(String[] args) throws ScriptException, NoSuchMethodException {接口
//建立腳本引擎管理類圖片
ScriptEngineManager manager=new ScriptEngineManager();
//查詢可用的腳本引擎
List<ScriptEngineFactory> factoryList=manager.getEngineFactories();
for(ScriptEngineFactory factory:factoryList){
String name=factory.getEngineName();
String language=factory.getLanguageName();
System.out.println(name+"-->"+language);
System.out.println("================================================");
//獲取javaScript腳本引擎
ScriptEngine jsEngine=manager.getEngineByName("JavaScript");
//向腳本引擎傳入變量
jsEngine.put("k", 100);
//計算表達式
jsEngine.eval("k=k*3+24");
//從腳本引擎查詢變量
Object k=jsEngine.get("k");
System.out.println(k);
System.out.println("================================================");
jsEngine.eval("function sayHello(){ println('hello,World!!!!'); }");
//調用腳本中的函數
((Invocable)jsEngine).invokeFunction("sayHello");
//或
jsEngine.eval("sayHello();"); 圖片滾動代碼
http://www.huiyi8.com/gundongdaima/
//腳本中使用java(下面代碼展現如何在腳本中使用java建立窗體)
String script="var jf=new javax.swing.JFrame();"
+ "jf.setTitle('hello,Script');"
+ "jf.setBounds(200,100,400,300);"
+ "jf.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);"
+ "jf.setVisible(true);";
jsEngine.eval(script);
2.package com.hongyuan.test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyTest {
public static void main(String[] args) {
//獲取接口代理對象
Demo demo=(Demo)Proxy.newProxyInstance(Demo.class.getClassLoader(),
new Class[]{Demo.class},new InvocationHandler() {
//代理對象上的方法調用時會轉交到此方法進行處理
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result=null;
System.out.println(method.getName()+"-->方法調用前!!!");
if("add".equals(method.getName())){
int sum=0;
for(int i=0,length=args.length;i<length;i++){
sum+=(int)args[i];
result=sum;
}else if("sayHello".equals(method.getName())){
System.out.println("hello,World!!!");
}else{
throw new Exception("你調用的方法還沒有實現!!!");
System.out.println(method.getName()+"-->方法調用後!!!");
return result;
System.out.println(demo.add(12, 12));
demo.sayHello();
demo.makeMoney();
interface Demo{
public int add(int num1,int num2);
public void sayHello();
public void makeMoney();