【问题标题】:Pharo get all setter from a classPharo 从一个班级获得所有的二传手
【发布时间】:2022-10-15 22:48:33
【问题描述】:

我是pharo的新手。我有个问题。我想从这种语言的课程中获得所有的二传手。但我不知道这样做。 例如,我们有 ClassA 有一个方法,如:

Object SubClass: #ClassA
    instanceVariableNames: 'name age'
    classVariableNames: ''
    package: 'MyPackage'
name:aName
    name:= aName
age:anAge
    age:= anAge

我有一个协议设置器。

如何在其他类中获取两个 setter 方法?

【问题讨论】:

  • 我不明白您提供的方法签名与“其他类中的两个 setter 方法”有何关系。另外,您想知道如何使用代码或 GUI 查找方法吗?
  • 我只想得到一个班级的所有二传手。我不知道pharo中是否有方法可以实现这一点。
  • 我在 pharo 中找到了一个方法,它返回一个类中的所有本地选择器。例如,Person localSelectors 提供了 Person 类中的所有选择器。这还不错。但我想要的是只让设置器实例化一个类。
  • 什么是“二传手”?你如何定义“二传手”? “得到”是什么意思?您是否在问如何使用 GUI 查找方法?还是使用代码?
  • setter 是用于修改对象私有属性的方法。我想知道如何使用源代码访问它的方法。

标签: smalltalk pharo


【解决方案1】:

我认为主要问题是“得到”是什么意思

如果你想为一个类创建 setter/getter,你需要创建它们。例如,如果您有一个 Class Person,那么您需要创建 setter #age: 和 getter #age

如果您想查看该类可用的所有访问器,您需要检查该类的协议。 accessing 是一种协议,您可以在其中找到使您能够访问对象内部值的消息。

如果要列出某些协议中可用的所有消息,可以使用(@Pharo 10):

| selectors |
selectors := OrderedCollection allSelectorsInProtocol: #'accessing'.

这将为您提供以下结果:

a Set(#yourself #enclosedElement #atPin: #before: #first:
#lastIndexOf:startingAt:ifAbsent: #fourth #replaceAll:with: #basicSize
#allButFirst #at:ifAbsent: #atLast: #allButFirst: #lastIndexOf: #size
#indexOfAnyOf:startingAt: #ninth #before:ifAbsent: #at:put: #last
#after:ifAbsent: #basicAt:put: #at: #indexOf:ifAbsent: #atLast:put:
#indexOfAnyOf: #atAll: #replaceFrom:to:with:startingAt: #nextToLast
#replaceFrom:to:with: #middle #first #fifth #at:incrementBy:
#lastIndexOf:ifAbsent: #allButLast: #indexOfSubCollection:startingAt: #atAllPut:
#indexOf:startingAt: #basicAt: #identityIndexOf: #atAll:putAll: #third #indexOf:
#atLast:ifAbsent: #last: #after: #capacity #anyOne #seventh #allButLast
#lastIndexOfAnyOf:startingAt:ifAbsent: #sixth #second #eighth #atAll:put:
#from:to:put: #indexOfAnyOf:ifAbsent: #atWrap:put:
#indexOfAnyOf:startingAt:ifAbsent: #indexOf:startingAt:ifAbsent: #swap:with:
#identityIndexOf:ifAbsent: #atWrap: #indexOfSubCollection:startingAt:ifAbsent:)

【讨论】:

  • 好的,我将尝试列出访问协议中的方法。非常感谢您。如果它有效,我会回复你。
  • 嗨@tukan,一旦我获得了定义明确的协议中的方法列表,我该如何使用它?
  • @MansourFaouziel 这取决于你想做什么。在获得此类列表之前,您通常应该知道这一点。
  • 嗨@tukan!在我的情况下,我希望检索给定类的访问协议中的方法,因为我希望从包含值的集合中访问对象的私有属性,以便能够用这些实例化它价值观。
  • @MansourFaouziel 这不是您想要的方式。您想创建类方法(它是一个构造器)并在其中通过实例变量#initialize 对象的值。我建议阅读有关 Smalltalk 的初学者书籍,例如esug.org/data/Lectures/Smalltalk-Lectures.pdf
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 2017-11-05
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
相关资源
最近更新 更多