【发布时间】:2016-11-10 01:39:03
【问题描述】:
我目前正在为 java 做一个项目,老师不太擅长解释自己。 目前我一直试图在java中引用一个扩展类来获得平衡。 (我正在尝试编辑余额,以便它可以在数组中显示和更改。
我的主账号是这样的
public class Account {
private String name;
private double quantity, price;
private double rate;
private Asset[] Asset;
}
被扩展的帐户是
abstract public class Asset {
String symbol;
public String getSymbol() {
return symbol;
}
protected Asset(String symbol) {
this.symbol = symbol;
}
}
扩展看起来像这样;
public class Cash extends Asset {
private double Quantity;
public Cash(double Quantity, String symbol) {
super(symbol);
this.Quantity = Quantity;
}
@Override
public String getSymbol() {
return super.getSymbol(); //To change body of generated methods, choose Tools | Templates.
}
public double getQuantity() {
return Quantity;
}
public void setQuantity(double Quantity) {
this.Quantity = Quantity;
}
}
现在假设我有一个由 Account 类创建的测试实例。 我如何能够编辑班级账户中的现金值? (主要参数) 以下是我的其他扩展,仅供参考。
public class Stock extends Asset {
private String name;
private double quantity, rate;
public Stock(String name, double quantity, double rate, String symbol) {
super(symbol);
this.name = name;
this.quantity = quantity;
this.rate = rate;
}
@Override
public String getSymbol() {
return super.getSymbol(); //To change body of generated methods, choose Tools | Templates.
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
如果有帮助,我将如何更改扩展类中的值? 我试过了
Asset[0] = new Cash(25000.00,string);
但这仅有助于手动输入值。 对于任何错误,我深表歉意,我对编码和 Java 很陌生。
【问题讨论】:
-
从根本上说,不要。如果您需要知道您的
Cash是Cash,则将其视为Cash,而不是通用Asset。 -
必须是一个,教授要求。
-
然后使用提供的答案,但请注意这是一个糟糕的设计。
标签: java inheritance subclass extend super