大佬教程收集整理的这篇文章主要介绍了GNU Smalltalk-继承和多参数方法/构造函数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
abstractMethod [
self subclassResponsibility
]
现在,当有人向您的类发送消息时,他会得到一个错误,指出应该实现此方法,并且您必须在子类中重写它。
是。子类可以访问所有实例变量。
好的,因此关键字消息之类的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
。然后,为了使您的生活更好,您可以进行参数设置new
并init
从那里调用参数设置。
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)
]
]
几个问题:
以上是大佬教程为你收集整理的GNU Smalltalk-继承和多参数方法/构造函数全部内容,希望文章能够帮你解决GNU Smalltalk-继承和多参数方法/构造函数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。