简体   繁体   English

如何获得协议中的所有方法?

[英]How can I get all the methods in a Protocol?

How can I get a collection of all the (class) methods in a given protocol in smalltalk/squeak/pharo? 如何在smalltalk / squeak / pharo中获取给定协议中所有(类)方法的集合?

I'm trying to collect the values returned by a group of methods. 我正在尝试收集一组方法返回的值。 I don't want to have to store the methods in an instance or class variable. 我不想将方法存储在实例或类变量中。 So I though I could add them to a protocol and in this way to "mark" them. 所以我虽然可以将它们添加到协议中并以这种方式“标记”它们。

Thanks. 谢谢。

In Pharo, the method you're looking for is ClassDescription>>allMethodsInCategory: : 在Pharo中,您正在寻找的方法是ClassDescription>>allMethodsInCategory: ::

| selectors |
selectors := MyClass allMethodsInCategory: #'protocol name'.

To find methods in a class-side protocol, just send to the metaclass instead: 要在类端协议中查找方法,只需发送到元类:

selectors := MyClass class allMethodsInCategory: #'protocol name'.

Another solution you might want to consider, though, is to use a pragma to mark your methods instead. 但是,您可能需要考虑的另一个解决方案是使用pragma标记您的方法。 See the comment on the Pragma class for details of that approach. 有关该方法的详细信息,请参阅Pragma类的注释。 It has the advantages that other packages can freely add methods belonging to your group (which need to be in a * protocol), and that the pragma can be used to store other metadata as well (such as an evaluation order, for example). 它的优点是其他包可以自由添加属于您的组的方法(需要在*协议中),并且该pragma也可以用于存储其他元数据(例如,评估顺序)。

NB. NB。 The selector allMethodsInCategory: has been deprecated in Pharo 3.0 and later in favor of allSelectorsInProtocol: 选择器allMethodsInCategory:已在Pharo 3.0及更高版本中弃用,支持allSelectorsInProtocol:

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

相关问题 Pharo:我可以手动而不是按字母顺序对协议中的方法进行排序吗? - Pharo: can I sort methods in a protocol manually instead of alphabetically? squeak(smalltalk) 如何获取对象的所有方法(继承方法也是如此) - squeak(smalltalk) how to get all the methods of an object (inherited methods too) 如何在Smalltalk中在运行时向类添加方法? - How can I add methods to a class at runtime in Smalltalk? Pharo Smalltalk-如何检查消息是否符合另一个对象的Class中定义的协议? - Pharo Smalltalk - How can I check if a message conforms to a protocol defined in another object's Class? Smalltallk - 如何在类的对象(当前实例)中获取所有实例变量的数组(或集合)? - Smalltallk - How can I get an Array (or Collection) of the all the Instance variables in an Object (the current Instance) of a Class? 如何获取表单所有字段的值? - How do I get the values of all the fields of a form? 如何找到引用myObject的所有对象? - How can I find all objects that reference myObject? "如何在 Smalltalk 中找到所有可用的方法并按名称搜索?" - How to find all methods available in Smalltalk and search by name? 我可以使用我的方法扩展内置的String类 - Can I extend built-in String class with my methods 我如何在Smalltalk中换行? - How can i get a new line in smalltalk?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM