如何在Smalltalk中调用带参数的方法

时间:2018-05-10 09:33:53

标签: smalltalk

我是编程语言Smalltalk的新手,我似乎无法弄清楚如何使用参数调用方法。我一直在玩一些代码并创建了一些方法,例如(在GNU Smalltalk中):

bin: n num: k [
      | i |
      i := 1.
      1 to:k do:[:j|
          i := i * 2.
      ].
      ^i
  ]

我现在想调用这个函数并实际得到一个答案,比如:bin:4 num:2(不知道怎么做)。我怎样才能做到这一点?在创建像我这样的方法时写“bin:n num:k”是否正确?

提前致谢!

1 个答案:

答案 0 :(得分:4)

首先,您需要一个接收器对象,您要在该对象上调用该方法。您没有指出您在哪个类中创建了方法,因此我假设您将其称为@Injectable() export class SearchService { getUrl: String = './../assets/promotionList.json'; subject: BehaviorSubject<Promotion[]> = new BehaviorSubject([]); // initialize with an empty response[] subjectAsObservable; someResult; promoList:Promotion[]; constructor(private http: HttpClient) { this.getPromoList(); console.log("after first"); this.getPromoValues(); console.log("after second call"); } getPromoList(){ // by default it emits a response of Json and hence Observabpe<PromotionList> // throws an error this.someResult = this.http.get(`${this.getUrl}`); console.log("before map"+<Observable<Promotion[]>> this.someResult); return (<Observable<Promotion[]>> this.someResult); //console.log("first subsriber"+JSON.stringify (this.someResult); }

MyClass

然后你就可以发送那条消息给(调用那个方法)myObject,如下所示:

| myObject |
myObject := MyClass new.

所以你只需在接收者之后写一条消息send(将调用该方法)。