GNU Smalltalk - Inheritance and Multiple Parameter Methods/Constructors

前端 未结 1 857
孤城傲影
孤城傲影 2021-01-14 17:35

Say I\'m trying to translate the below Java classes to GNU Smalltalk:

public abstract class Account {

    protected String number;
    protected Customer cu         


        
相关标签:
1条回答
  • 2021-01-14 17:57
    1. You shouldn't bother with some kind of "making class abstract" :). But the closest solution to your question is

      abstractMethod [
          self subclassResponsibility
      ]
      

      Now when someone sends a message to your class he'll get an error that this method should be implemented, and you must override it in subclasses.

    2. Yes. All instance vars can be accessed by a subclass.

    3. Ok, so the keyword messages like withdraw: amount can actually have multiple parameters like: withdraw: amount becauseOf: reason. So first of all you make an initialiser:

      initWithBalance: aBalance customer: aCustomer number: aNumber [ 
          self init.
          balance := aBalance.
          customer := aCustomer.
          number := aNumber
      ]
      

      You can keep interest := 0. in main init. Then, to make your life better, you make a parameterised new and call parameterised init from there.

      SavingsAccount class [
          newWithBalance: aBalance customer: aCustomer number: aNumber [
             ^ self new initWithBalance: aBalance customer: aCustomer number: aNumber
          ]
      ]
      
    0 讨论(0)
提交回复
热议问题