我想使用重构浏览器以编程方式将实例变量添加到现有类:
| theClass className |
className := #MyClass.
theClass := (RBClass existingNamed: className)
model: (RBNamespace new classNamed: className; yourself);
yourself.
theClass addInstanceVariable: 'testIVar'
但是没有使用新的实例变量修改类,我缺少什么?
答案 0 :(得分:6)
您忘了执行重构。试试这个
| model className theClass iVarName |
className := #MyClass.
iVarName := 'testIVar'.
model := RBNamespace new classNamed: className; yourself.
theClass := (RBClass existingNamed: className)
model: model;
yourself.
(RBAddInstanceVariableRefactoring
model: model
variable: iVarName
class: theClass) execute.
你可能想为你的新实例变量添加authomatic accession方法(getter和setter)
(RBCreateAccessorsForVariableRefactoring
model: model
variable: iVarName
class: theClass
classVariable: false) execute