IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

三十四、Groovy 元对象编程

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=8485

元对象编程或MOP可以用于动态调用方法,并且可以即时创建类和方法。

那么这是什么意思呢?让我们考虑一个叫Student的类,它是一个没有成员变量或方法的空类。假设你必须在这个类上调用以下语句。

Def myStudent = new Student() 
myStudent.Name = ”Joe”; 
myStudent.Display()

现在在元对象编程中,即使类没有成员变量Name或方法Display(),上面的代码仍然可以工作。

这如何工作?那么,为了这个工作,一个人必须实现GroovyInterceptable接口挂钩到Groovy的执行过程。以下是该接口的可用方法。

Public interface GroovyInterceptable { 
   Public object invokeMethod(String methodName, Object args) 
   Public object getproperty(String propertyName) 
   Public object setProperty(String propertyName, Object newValue) 
   Public MetaClass getMetaClass() 
   Public void setMetaClass(MetaClass metaClass) 
}

所以在上面的接口描述中,假设你必须实现invokeMethod(),它会被调用的每个方法,要么存在或不存在。

缺失属性

所以,让我们看一个例子,我们如何为缺失的属性实现元对象编程。以下键应该注意以下代码。

  • 类Student没有定义名为Name或ID的成员变量。
  • 类Student实现GroovyInterceptable接口。
  • 有一个称为dynamicProps的参数,将用于保存即时创建的成员变量的值。
  • 方法getproperty和setproperty已被实现以在运行时获取和设置类的属性的值。
class Example {
   static void main(String[] args) {
      Student mst = new Student();
      mst.Name = "Joe";
      mst.ID = 1;

      println(mst.Name);
      println(mst.ID);
   }
}

class Student implements GroovyInterceptable { 
   protected dynamicProps=[:]

   void setProperty(String pName,val) {
      dynamicProps[pName] = val
   }

   def getProperty(String pName) {
      dynamicProps[pName]
   } 
} 

以下代码的输出将是 –

Joe 
1

缺失方法

所以,让我们看一个例子,我们如何为缺失的属性实现元对象编程。以下键应该注意下面的代码 –

  • 类学生现在实现invokeMethod方法,它被调用,而不管该方法是否存在。
class Example {
   static void main(String[] args) {
      Student mst = new Student();
      mst.Name = "Joe";
      mst.ID = 1;

      println(mst.Name);
      println(mst.ID);
      mst.AddMarks();
   } 
}

class Student implements GroovyInterceptable {
   protected dynamicProps = [:]  

   void setProperty(String pName, val) {
      dynamicProps[pName] = val
   } 

   def getProperty(String pName) {
      dynamicProps[pName]
   }

   def invokeMethod(String name, Object args) {
      return "called invokeMethod $name $args"
   }
}

以下代码的输出如下所示。请注意,即使方法Display不存在,也没有缺少方法异常的错误。

Joe 
1 

元类

此功能与MetaClass实现相关。在默认实现中,您可以访问字段而不调用它们的getter和setter。以下示例显示如何通过使用metaClass函数,我们能够更改类中的私有变量的值。

class Example {
   static void main(String[] args) {
      Student mst = new Student();
      println mst.getName()
      mst.metaClass.setAttribute(mst, 'name', 'Mark')
      println mst.getName()
   } 
} 

class Student {
   private String name = "Joe";

   public String getName() {
      return this.name;
   } 
}

以下代码的输出将是 –

Joe 
Mark

方法缺失

##

Groovy支持methodMissing的概念。此方法与invokeMethod的不同之处在于,它仅在失败的方法分派的情况下被调用,当没有找到给定名称和/或给定参数的方法时。以下示例显示如何使用methodMissing。

class Example {
   static void main(String[] args) {
      Student mst = new Student();
      mst.Name = "Joe";
      mst.ID = 1;

      println(mst.Name);
      println(mst.ID);
      mst.AddMarks();
   } 
} 

class Student implements GroovyInterceptable {
   protected dynamicProps = [:]  

   void setProperty(String pName, val) {
      dynamicProps[pName] = val
   }

   def getProperty(String pName) {
      dynamicProps[pName]
   }

   def methodMissing(String name, def args) {         
      println "Missing method"
   }  
}

以下代码的输出将是 –

Joe 
1 
Missing method 

干货推荐


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(77) 打赏



未经允许不得转载:搜云库技术团队 » 三十四、Groovy 元对象编程

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367