程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了GNU Smalltalk-继承和多参数方法/构造函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决GNU smalltalk-继承和多参数方法/构造函数?

开发过程中遇到GNU smalltalk-继承和多参数方法/构造函数的问题如何解决?下面主要结合日常开发的经验,给出你关于GNU smalltalk-继承和多参数方法/构造函数的解决方法建议,希望对你解决GNU smalltalk-继承和多参数方法/构造函数有所启发或帮助;
  1. 您不应该为某种“使类抽象化”而烦恼:)。但是最接近您问题的解决方案是
    abstractMethod [
    self subclassResponsibility
    

    ]

现在,当有人向您的类发送消息时,他会得到一个错误,指出应该实现此方法,并且您必须在子类中重写它。

  1. 是。子类可以访问所有实例变量。

  2. 好的,因此关键字消息之类的withdraw: amount实际上可以具有多个参数,例如:withdraw: amount becauSEOf: reason。因此,首先要进行初始化:

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

    ]

您可以保持interest := 0.@H_7_6@main init。然后,为了使您的生活更好,您可以进行参数设置newinit从那里调用参数设置。

    SavingsAccount class [
    newWithBalance: aBalance customer: aCustomer number: anumber [
       ^ self new initWithBalance: aBalance customer: aCustomer number: anumber
    ]
]

解决方法

假设我正在尝试将以下Java类转换为GNU smalltalk:

public abstract class Account {

    protected String number;
    protected Customer customer;
    protected double balance;

    public abstract void accrue(double ratE);

    public double balance() {
        return balance;
    }

    public void deposit(double amount) {
        balance += amount;
    }

    public void withdraw(double amount) {
        balance -= amount;
    }

    public String toString() {
        return number + ":" + customer + ":" + balance;
    }
}

public class SavingsAccount extends Account {

    private doublE interest = 0;

    public SavingsAccount(String number,Customer customer,double balancE) {
        this.number = number;
        this.customer = customer;
        this.balance = balance;
    }

    public void accrue(double ratE) {
        balance += balance * rate;
        interest += interest * rate;
    }

}

我正在努力了解如何编写带有多个参数的方法/构造函数。到目前为止,这是我得到的:

Object subclass: Account [

    |number customer balance|

    balance [
        ^balance
    ]

    deposit: amount [
         balance := balance + amount
    ]

    withdraw: amount [
        balance := balance - amount
    ]

    asString [
        ^number asString,':',customer asString,balance asString
    ]

]

Account subclass: SavingsAccount [

    |interest|

    SavingsAccount class [
        new [ "add some sort of support for multiple arguments?"
           "call init"
        ]
    ]

    init [ "add some sort of support for multiple arguments?"
         interest := 0.
         balance := accountBalance.
         customer := accountCustomer.
         number := accountnumber
    ]

    accrue: rate [
        balance := balance + (balance * ratE).
        interest := interest + (interest * ratE)
    ]

]

几个问题:

  1. 如何使Account成为smalltalk中的抽象类?
  2. 我假设所有的Account实例变量都可以通过SavingsAccount类中的名称进行访问吗?
  3. 如何实现类似于Java SavingsAccount类中的多参数构造函数的东西?

大佬总结

以上是大佬教程为你收集整理的GNU Smalltalk-继承和多参数方法/构造函数全部内容,希望文章能够帮你解决GNU Smalltalk-继承和多参数方法/构造函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。