java打印接口的ArrayList。

huangapple go评论82阅读模式
英文:

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&lt;Observer&gt; observers;
	private double ibmPrice;
	private double aaplPrice;
	private double googPrice; 
	
	public StockGrabber(){
		
		// Creates an ArrayList to hold all observers

		observers = new ArrayList&lt;Observer&gt;();
		
	}
	
	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&#39;t work
        toString();
	}

	public void printThePrices(){
		
		System.out.println(observerID + &quot;\nIBM: &quot; + ibmPrice + &quot;\nAAPL: &quot; + 
				aaplPrice + &quot;\nGOOG: &quot; + googPrice + &quot;\n&quot;);
		
	}
	
	public String toString() {
	    return &quot;StockObserver: ibmPrice=&quot; + ibmPrice + &quot; aaplPrice=&quot; + 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&lt;Observer&gt; 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 &quot;StockObserver: ibmPrice=&quot; + ibmPrice + &quot; aaplPrice=&quot; + aaplPrice;
}

</details>



huangapple
  • 本文由 发表于 2020年4月6日 23:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61062706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定