gosu - Gosu类vs增强
问题描述
我想知道Gosu类和增强之间的区别。因为无论我们在增强中可以做什么,我们可以在 Gosu 课上做什么,那么 Gosu Enhancement 的需要是什么。
解决方案
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
希望这可以帮助。
阿拉文德:)
推荐阅读
- jmeter - Jmeter工具,线程循环计数是永远的,线程数1以增量方式跳过循环控制器中的html页面
- google-app-engine - 特定服务的 AppEngine 防火墙
- html - CSS 根据屏幕大小更改内容
- python - 连接两个文件并根据另一列填充新列
- mysql - sql查询仅适用于1条记录
- angular - 使用 Angular 和 Web API 保存图像和视频的最佳方法
- c# - 插座。正确获得响应
- kendo-ui - 有条件地选中/取消选中 Kendo Grid Columns
- javascript - Sails.js 响应不返回对象字段
- mongodb - MongoDB每天为每个用户显示第一个和最后一个事件