英文:
java print arraylist of interface
问题
// Creates an ArrayList to hold all observers
observers = new ArrayList<Observer>();
// ...
public void notifyObserver() {
for(Observer observer : observers){
observer.updateViaObserver(ibmPrice, aaplPrice, googPrice);
}
}
// Doesn't work
toString();
// 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();
}
// Accessing the ArrayList elements inside notifyObserver method
System.out.println(observers.get(0));
英文:
I'm learning observer design pattern from one of youtube videos and want to understand a bit more the behavior of interface arraylist, I think I quite understand how it works but when it comes the interface arraylist, its confusing to me how to access the value inside loop.
the subject and observer interfaces as follow;
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;
}
}
MAIN
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();
}
}
How to access value for my ArrayList<Observer> observers
inside "notifyObserver" method? if I do this System.out.println(observers.get(0));
I get observer_pattern.StockObserver@446cdf90
答案1
得分: 0
如何在 "notifyObserver" 方法内部访问我的 ArrayList<Observer> observers 的值?
你已经在做了。
public void notifyObserver() {
for (Observer observer : observers) {
observer.updateViaObserver(ibmPrice, aaplPrice, googPrice);
}
}
如果我执行 System.out.println(observers.get(0));,我会得到 observer_pattern.StockObserver@446cdf90。
这是因为你的 `StockObserver` 类没有一个返回对象的可读表示的 `toString` 方法。可以像这样做:
public String toString() {
return "StockObserver: ibmPrice=" + ibmPrice + " aaplPrice=" + aaplPrice;
}
英文:
> How to access value for my ArrayList<Observer> observers inside "notifyObserver" method?
You're already doing it.
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
That's because your StockObserver
class does not have a toString
method that returns a human readable representation of the object. Something like this
public String toString() {
return "StockObserver: ibmPrice=" + ibmPrice + " aaplPrice=" + aaplPrice;
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论