gpt4 book ai didi

java打印接口(interface)的数组列表

转载 作者:行者123 更新时间:2023-12-01 17:34:34 25 4
gpt4 key购买 nike

我正在从 YouTube 视频之一中学习观察者设计模式,并且想要更多地了解接口(interface) arraylist 的行为,我想我很理解它是如何工作的,但是当涉及到接口(interface) arraylist 时,它让我感到困惑如何访问循环内的值。

主体和观察者界面如下;

public interface Subject {

public void register(Observer o);
public void unregister(Observer o);
public void notifyObserver();

}
public interface Observer {

public void updateViaObserver(double ibmPrice, double aaplPrice, double googPrice);

}
package observer_pattern;

import java.util.ArrayList;
import java.util.Arrays;

public class StockGrabber implements Subject{

private ArrayList<Observer> observers;
private double ibmPrice;
private double aaplPrice;
private double googPrice;

public StockGrabber(){

// Creates an ArrayList to hold all observers

observers = new ArrayList<Observer>();

}

public void register(Observer newObserver) {

observers.add(newObserver);


}

public void notifyObserver() {

for(Observer observer : observers){

observer.updateViaObserver(ibmPrice, aaplPrice, googPrice);

}


}

public void setPrice(double newIBMPrice, double newAAPLPrice, double newGOOGPrice){

this.ibmPrice = newIBMPrice;
this.aaplPrice = newAAPLPrice;
this.googPrice = newGOOGPrice;

notifyObserver();

}


}
package observer_pattern;

public class StockObserver implements Observer {

private double ibmPrice;
private double aaplPrice;
private double googPrice;

private Subject stockGrabber;

public StockObserver(Subject stockGrabber){

this.stockGrabber = stockGrabber;

this.observerID = ++observerIDTracker;

stockGrabber.register(this);

}

// Called to update all observers

public void updateViaObserver(double ibmPrice, double aaplPrice, double googPrice) {

this.ibmPrice = ibmPrice;
this.aaplPrice = aaplPrice;
this.googPrice = googPrice;

// this works
printThePrices();


// doesn't work
toString();
}

public void printThePrices(){

System.out.println(observerID + "\nIBM: " + ibmPrice + "\nAAPL: " +
aaplPrice + "\nGOOG: " + googPrice + "\n");

}

public String toString() {
return "StockObserver: ibmPrice=" + ibmPrice + " aaplPrice=" + aaplPrice;
}

}

主要内容

package observer_pattern;

public class GrabStocks{

public static void main(String[] args){
StockGrabber stockGrabber = new StockGrabber();

StockObserver observer1 = new StockObserver(stockGrabber);

stockGrabber.setIBMPrice(197.00);
stockGrabber.setAAPLPrice(677.60);
stockGrabber.setGOOGPrice(676.40);

observer1.toString();



}

}

如何获取我的 ArrayList<Observer> observers 的值(value)在“notifyObserver”方法中?如果我这样做System.out.println(observers.get(0));我得到observer_pattern.StockObserver@446cdf90

最佳答案

How to access value for my ArrayList observers inside "notifyObserver" method?

你已经在这么做了。

public void notifyObserver() {
for(Observer observer : observers){
observer.updateViaObserver(ibmPrice, aaplPrice, googPrice);
}
}

if I do this System.out.println(observers.get(0)); I get observer_pattern.StockObserver@446cdf90

这是因为您的 StockObserver 类没有返回对象的人类可读表示形式的 toString 方法。像这样的事情

public String toString() {
return "StockObserver: ibmPrice=" + ibmPrice + " aaplPrice=" + aaplPrice;
}

关于java打印接口(interface)的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61062706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com