请大神帮忙看下这句代码什么意思啊? Stock stock = (Stock)stockList.get(0); 代码见

请大神帮忙看下这句代码什么意思啊? Stock stock = (Stock)stockList.get(0); 代码见下:
public void AddBuyInfo(BuyInfo buyInfo) throws Exception {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
/*根据商品编号获取到商品库存对象*/
int productId = buyInfo.getProduct().getProductId();
String hql = "From Stock stock where stock.product.productId=" + productId;
Query q = s.createQuery(hql);
/*计算当前显示页码的开始记录*/
List stockList = q.list();
Stock stock = (Stock)stockList.get(0);
/*更新商品库存信息*/
stock.setStockCount(stock.getStockCount() + buyInfo.getCount());
/*更新商品价格*/
stock.setStockprice(buyInfo.getPrice());
/*登记进货信息*/
s.merge(buyInfo);
/*更新库存信息*/
s.merge(stock);
tx.commit();
} catch (HibernateException e) {
if(tx != null)
tx.rollback();
throw e;
} finally {
HibernateUtil.closeSession();
}
}
maoyuhan 1年前 已收到1个回答 举报

沈家洛洛 幼苗

共回答了23个问题采纳率:91.3% 举报

Stock明显是一个JavaBean.里面是什么我就不知道了.
然后stockList是一个集合,那句代码就是获取这个集合里面的第一个元素.然后把这个获取到的对象强制类型转换成Stock,再赋值给stock变量.其中(class)这样的写法就是强制类型转换了,括号里面的就是需要转换的类型了.

1年前

2
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 1.103 s. - webmaster@yulucn.com