简体   繁体   中英

Gosu class vs enhancement

I want to know the difference between Gosu class and enhancement. Because whatever we can do in enhancement, that we can do in Gosu class also then what is the need of Gosu Enhancement.

Gosu class is just like a Java class. 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.

for example, lets say i need to write an function to check whether a number entered is greater than 10 or not.

So using gosu class, how we write the code is like

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 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. 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.

"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.

Find the Differences here follows.

区别如下

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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