Gosu类vs增强

问题描述 投票:2回答:4

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

gosu guidewire
4个回答
3
投票

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

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

例如,假设我需要编写一个函数来检查输入的数字是否大于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

希望这会有所帮助。

Aravind:)


1
投票

在增强功能中,不允许定义任何变量(记录无变化)。因此,增强应仅用于简单的聚合计算。增强的优点是新方法在实体中可见。如果您在Gosu类中定义,则必须知道类名称。


1
投票

在下面找到差异。

Difference here follows


0
投票

您可以将其假定为对象的扩展属性(在您要执行的对象或属性之上进行微调操作)

© www.soinside.com 2019 - 2024. All rights reserved.