Java学习_根据用户信息查看浏览过商品信息

学习内容:

通过用户类和商品类来设置和查询相关信息


学习代码:

package Example;
//新建一个用户类
class User{
	private String uid; 	//用户id
	private String uname;	//用户名字
	private Comm comms[];	//商品信息
	public void setComm(Comm comms[]) {
		this.comms=comms;
	}
	public User(String uid,String uname) {
		this.uid=uid;
		this.uname=uname;
	}
	public String getInfo() {
		return "【用户信息】用户ID="+this.uid+",姓名="+this.uname;
	}
	public Comm[] getComms() {
		return this.comms;
	}
}
//新建一个商品类
class Comm{
	private long cid;		//商品id
	private String cname;	//商品名字
	private long price;		//商品价格
	private User users[];	//用户信息
	public void setUser(User users[]) {
		this.users=users;
	}
	public Comm(long cid,String cname,long price) {
		this.cid=cid;
		this.cname=cname;
		this.price=price;
	}
	public User[] getUsers() {
		return this.users;
	}
	public String getInfo() {
		return "【商品信息】cid="+this.cid+",name="+this.cname+",price="+this.price;
	}
}
public class ep6_13_2 {

	public static void main(String[] args) {
		User useA=new User("boy","孙悟空");	//实例化用户A
		User useB=new User("girl","孙尚香");	//实例化用户B
		Comm comA=new Comm(1L,"复活甲",2080);	//实例化商品A
		Comm comB=new Comm(2L,"鞋子",600);	//实例化商品A
		Comm comC=new Comm(3L,"宝石",300);	//实例化商品A
		useA.setComm(new Comm[] {comA,comB,comC});  //用户A setComm赋值
		useB.setComm(new Comm[] {comA});
		comA.setUser(new User[] {useA,useB});
		comC.setUser(new User[] {useA,useB});
		System.out.println("---------根据用户信息查看浏览过商品信息---------------");
		System.out.println(useA.getInfo());
		for(int x=0;x<useA.getComms().length;x++) {
			System.out.println("\t|-"+useA.getComms()[x].getInfo());
		}
		System.out.println("---------根据商品信息查看浏览过商品的用户---------------");
		System.out.println(comA.getInfo());
		for(int x=0;x<comA.getUsers().length;x++) {
			System.out.println("\t|-"+comA.getUsers()[x].getInfo());
		}
	}

}

运行截图:

Java学习_根据用户信息查看浏览过商品信息

上一篇:搭建rtmp直播流服务之2:使用java实现ffmpeg命令接口化调用(用java执行ffmpeg命令)


下一篇:Oracle操作