简体   繁体   English

Gosu 类 vs 增强

[英]Gosu class vs enhancement

I want to know the difference between Gosu class and enhancement.我想知道 Gosu 类和增强之间的区别。 Because whatever we can do in enhancement, that we can do in Gosu class also then what is the need of Gosu Enhancement.因为无论我们在增强中可以做什么,我们在 Gosu 类中也可以做的,那么 Gosu 增强的需要是什么。

Gosu class is just like a Java class. Gosu 类就像一个 Java 类。 What confuses you is the enhancement.令您困惑的是增强功能。

Enhancements are the extended properties of an OBJECT and are available for particular objects for which it is written for.增强是 OBJECT 的扩展属性,可用于为其编写的特定对象。

for example, lets say i need to write an function to check whether a number entered is greater than 10 or not.例如,假设我需要编写一个函数来检查输入的数字是否大于 10。

So using gosu class, how we write the code is like所以使用 gosu 类,我们如何编写代码就像

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

and we call the function like:我们调用函数如下:

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

So basically , the class and the method which we wrote are available anywhere in our application.所以基本上,我们编写的类和方法在我们的应用程序中的任何地方都可用。 Here comes the use of Enhancement这里是使用Enhancement

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

The above enhancement is available for Integer objects only.上述增强仅适用于 Integer 对象。 and all the functions inside this enhancement becomes the property of any integer object.并且此增强中的所有函数都成为任何整数对象的属性。

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

The call be made even simpler like呼叫变得更简单,例如

36.isNoGreaterThan10() //return True

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

Similarly, lets see an enhancement for a string (say to get the length of a string)同样,让我们​​看看字符串的增强(比如获取字符串的长度)

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

and the property Length() will be available for all string objects.并且属性 Length() 将可用于所有字符串对象。

"Hello boss".Length // returns 10

hope this helps.希望这可以帮助。

Aravind :)阿拉文 :)

In an enhancement, it is not allowed to define any variable (No change for logging).在增强中,不允许定义任何变量(日志记录无更改)。 Thus, enhancement should be used for simple aggregate calculation only.因此,增强应该仅用于简单的聚合计算。 The advantage from enhancement is the new method is visible from the entity.增强的优点是新方法从实体中可见。 If you define in Gosu class, you must know the class name.如果在 Gosu 类中定义,则必须知道类名。

Find the Differences here follows.找出不同之处如下。

区别如下

您可以将其视为对象的扩展属性(在您希望执行的对象或属性之上的微调操作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM