1 先创建Stock类,定义它的属性
package com.dr.Stock;
public class Stock {
private int id;
private String name;
private int price;
private int flag;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
}
2创建StockServer类 服务器端
package com.dr.Stock;
import java.util.Random;
public class StockServer {
public static Stock[] getStock(){
Stock stock[]=new Stock[3];
//#####################################
Stock sk=new Stock();
sk.setId(1);
sk.setName("ibm");
Random rd=new Random();
sk.setPrice(rd.nextInt(100));
sk.setFlag(1);
stock[0]=sk;
//##################################
Stock sk1=new Stock();
sk1.setId(2);
sk1.setName("sun");
sk1.setPrice(rd.nextInt(100));
sk1.setFlag(1);
stock[1]=sk1;
//##################################
Stock sk2=new Stock();
sk2.setId(3);
sk2.setName("oracle");
sk2.setPrice(rd.nextInt(100));
sk2.setFlag(1);
stock[2]=sk2;
return stock;
}
}
3创建StockClient类 作为客户端 进行测试
package com.dr.Stock;
public class StockClient {
public static void main(String[] args) {
while(true){
Stock sk[]=StockServer.getStock();
for(int i=0;i<sk.length;i++){
System.out.println("股票号:"+sk[i].getId()+" 股票名:"+sk[i].getName()+" 股票类型:"+sk[i].getFlag()+" 股票价格:"+sk[i].getPrice());
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
}