事物模型和对象一样也可以具有行为,具体可以参看X-Meta引擎的相关文档。
self变量
这里只是在说明一下self变量,一个事物的行为的实现通常是Java代码或Groovy等脚本代码,在代码里可以使用一个名为self的变量,这个变量就是事物模型本身。
比如事物模型:
<Person name="Tom" age="40">
<Child name="Smith" age="10"/>
<actions name="actions" descriptors="actions">
<JavaAction name="run" useOuterJava="true" descriptors="JavaAction"
methodName="run" outerClassName="org.xmeta.example.PersonWithActionTest"/>
</actions>
</Person>
Person模型的run方法的的实现是:
/**
* 事物的行为实现。
*
* @param actionContext
*/
public static void run(ActionContext actionContext){
Thing self = (Thing) actionContext.get("self");
//属性
System.out.println("name=" + self.get("name"));
System.out.println("age=" + self.get("age"));
//子事物
for(Thing child : self.getChilds()){
System.out.println("child name=" + child.get("name"));
System.out.println("child age=" + child.get("age"));
}
}
需要注意的是self变量是一个动作当做行为时执行时加入的,仅仅当做动作执行时则没有self变量。
Copyright © 2007-2019 XWorker.org 版权所有 沪ICP备08000575号