首页 > 解决方案 > Gosu类vs增强

问题描述

我想知道Gosu类和增强之间的区别。因为无论我们在增强中可以做什么,我们可以在 Gosu 课上做什么,那么 Gosu Enhancement 的需要是什么。

标签: gosuguidewire

解决方案


Gosu 类就像一个 Java 类。使您感到困惑的是增强功能。

增强是 OBJECT 的扩展属性,可用于为其编写的特定对象。

例如,假设我需要编写一个函数来检查输入的数字是否大于 10。

所以使用 gosu 类,我们如何编写代码就像

Class MyInteger(){
    static funtion isNoGreaterThan10(no : int) : boolean{
      return (no > 10) 
    }
}

我们称这个函数为:

MyInteger.isNoGreaterThan10(34) //returns a boolean value

所以基本上,我们编写的类和方法在我们的应用程序的任何地方都可用。增强功能的使用

Enhancement MyInteger : int{
       funtion isNoGreaterThan10() : boolean{
          return (this > 10) //"this" represents the object upon which we are calling this enhancement
        }
}

上述增强功能仅适用于 Integer 对象。并且此增强中的所有功能都成为任何整数对象的属性。

var number = 14
number.isNoGreaterThan10() //return True

调用变得更加简单,例如

36.isNoGreaterThan10() //return True

"my_name".isNoGreaterThan10() // is not possible as "my_name" is not an integer.

同样,让我们​​看看对字符串的增强(比如获取字符串的长度)

Enhancement MyStringEnhancement : String {
  property get Length():int{
    return len(this)
  }
}

并且属性 Length() 将可用于所有字符串对象。

"Hello boss".Length // returns 10

希望这可以帮助。

阿拉文德:)


推荐阅读