Java继承问题
我是编程新手,我的程序出现问题,应返回此输出:Java继承问题,java,Java,我是编程新手,我的程序出现问题,应返回此输出: Portfolio #00001, ASD = 42.50, DFAS = 45.00, CAC = 22.20, BDM = 52.50 Portfolio #00002, ASD = 42.50, DFAS = 45.00, CAC = 22.20, BDM = 52.50 Portfolio #00001, ASD = 43.35, DFAS = 45.90, CAC = 22.64, BDM = 53.55 Portfoli
Portfolio #00001, ASD = 42.50, DFAS = 45.00, CAC = 22.20, BDM = 52.50
Portfolio #00002, ASD = 42.50, DFAS = 45.00, CAC = 22.20, BDM = 52.50
Portfolio #00001, ASD = 43.35, DFAS = 45.90, CAC = 22.64, BDM = 53.55
Portfolio #00002, ASD = 43.35, DFAS = 45.90, CAC = 22.64, BDM = 53.55
Portfolio #00001, ASD = 41.18, DFAS = 43.61, CAC = 21.51, BDM = 50.87
Portfolio #00002, ASD = 41.18, DFAS = 43.61, CAC = 21.51, BDM = 50.87
Portfolio #00001, ASD = 43.65, DFAS = 46.22, CAC = 22.80, BDM = 53.92
Portfolio #00002, ASD = 43.65, DFAS = 46.22, CAC = 22.80, BDM = 53.92
目前,我的程序没有返回任何内容。有人能帮忙吗
以下是课程:
DisplayElement接口:
public interface DisplayElement {
public void display();
}
public interface Observer {
public void update (Map<String,Double> priceMap);
}
观察员界面:
public interface DisplayElement {
public void display();
}
public interface Observer {
public void update (Map<String,Double> priceMap);
}
公共接口观察者{
公共作废更新(地图价格地图);
}
设置价格应为:
public <map> void setPrices(Map<> priceMap) {
this.priceMap = priceMap; // <-- missing line
measurementsChanged();
}
public void设置价格(Map priceMap){
this.priceMap=priceMap;//根据观察者模式的要求,您需要在创建protofolios之后,在调用setPrices()之前注册protofolios:您的程序应该如下所示:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
interface DisplayElement {
void display();
}
interface Observer {
void update (Map<String,Double> priceMap);
}
class PricesDisplay implements Observer, DisplayElement {
private String ticker;
private double price;
private Subject PriceData;
Map<String,Double> priceMap;
PricesDisplay(Subject PriceData) {
this.PriceData = PriceData;
}
PricesDisplay(String ticker, Subject PriceData) {
this.ticker = ticker;
this.PriceData = PriceData;
}
public void update(Map<String,Double> priceMap) {
this.priceMap = priceMap;
display();
}
public void display() {
for (Map.Entry<String, Double> entry : priceMap.entrySet()) {
System.out.printf("\nPortfolio #%s, " + "%s = " + "%.2f, ",
ticker, entry.getKey(), entry.getValue());
}
}
}
interface Subject{
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
void measurementsChanged();
void setPrices(Map<String,Double> priceMap);
}
class PriceData implements Subject {
private ArrayList observers;
PriceData priceData;
private Map<String,Double> priceMap = new HashMap<String,Double>();
PriceData() {
observers = new ArrayList();
}
public void registerObserver(Observer o) {
observers.add(o);
}
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}
public void notifyObservers() {
for (int i = 0; i < observers.size(); i++) {
Observer observer = (Observer)observers.get(i);
observer.update(priceMap);
}
}
public void measurementsChanged() {
notifyObservers();
}
public void setPrices(Map<String,Double> priceMap) {
this.priceMap = priceMap;
measurementsChanged();
}
}
class Test {
private static Map<String,Double> priceMap = new HashMap<String,Double>();
private static PriceData priceData = new PriceData();
public static void main(String[] args) {
// establish two portfolios as listeners for priceData.
// for now, we assume that both portfolios contain the same
// collection of investments.
PricesDisplay firstPortfolio =
new PricesDisplay("00001", priceData);
PricesDisplay secondPortfolio =
new PricesDisplay("00002", priceData);
priceData.registerObserver(firstPortfolio);
priceData.registerObserver(secondPortfolio);
generateInitialPrices();
updatePrices(.02);
updatePrices(-.05);
updatePrices(.06);
}
static void generateInitialPrices()
{
priceMap.put("ASD", 42.50);
priceMap.put("BDM", 52.50);
priceMap.put("CAC", 22.20);
priceMap.put("DFAS", 45.00);
priceData.setPrices(priceMap);
}
static void updatePrices(double changePercent)
{
for( String key : priceMap.keySet())
{
double v = priceMap.get(key) * (1.0 + changePercent);
priceMap.put(key, v);
}
priceData.setPrices(priceMap);
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Map;
界面显示元素{
void display();
}
界面观察者{
无效更新(地图价格地图);
}
类PricesDisplay实现了Observer、DisplayElement{
专用字符串自动售票机;
私人双价;
私有主体价格数据;
地图价格地图;
价格显示(主题价格数据){
this.PriceData=PriceData;
}
价格显示(字符串代码、主题价格数据){
this.ticker=ticker;
this.PriceData=PriceData;
}
公共无效更新(地图价格地图){
this.priceMap=priceMap;
显示();
}
公共空间显示(){
对于(Map.Entry:priceMap.entrySet()){
System.out.printf(“\nPortfolio#%s,”+%s=“+”%.2f,”,
ticker、entry.getKey()、entry.getValue());
}
}
}
接口主题{
无效注册观察者(观察者o);
无效移除观察员(观察员o);
无效();
空隙测量改变();
无效设定价格(Map priceMap);
}
类PriceData实现主题{
私人ArrayList观察员;
价格数据价格数据;
private Map priceMap=new HashMap();
价格数据(){
Observators=新的ArrayList();
}
公共无效注册表观察者(o){
观察员:添加(o);
}
公共无效移除观察员(观察员o){
int i=观察者。indexOf(o);
如果(i>=0){
观察员:删除(i);
}
}
公营机构({
对于(int i=0;i
try firstPortfolio.display()在您的mainsetPrices(map PriceMap)中没有设置任何内容。您需要注册您的投资组合以启用模式我不确定我应该在setPrices(map PriceMap)中设置什么方法尝试我帖子中的代码,它对我有用(我不确定是否理解您的代码逻辑:)。但是PriceData#priceMap变量从未设置,因此始终为空。我为每个投资组合注册了观察员,但我在运行程序时仍收到一个错误。