同一个包中的两个类调用的时候,需要注意的问题。

展开阅读全文

关于同一个包中,两个调用。。。。出项问题

11-28

我在myeclipse下新建了一个package,在package下新建了两个类:具体代码如下:rn[code=Java]rnpackage db;rnimport java.io.Serializable;rnrnpublic class BookBean implements Serializablernrn private static final long serialVersionUID = 1L; rn private int id;rn private String title;rn private String author;rn private String bookconcern;rn private String publish_date;rn private float price;rn private int amount;rn private String remark;rnrn public BookBean()rn rn rn rn public BookBean(int id, String title, String author, String bookconcern,rn String publish_date, float price, int amount, String remark)rn rn this.id=id;rn this.title=title;rn this.author=author;rn this.bookconcern=bookconcern;rn this.publish_date=publish_date;rn this.price=price;rn this.amount=amount;rn this.remark=remark;rn rn rn public int getId()rn rn return id;rn rn rn public void setTitle(String title)rn rn this.title = title;rn rn rn public void setAuthor(String author)rn rn this.author = author;rn rn rn public void setBookconcern(String bookconcern)rn rn this.bookconcern = bookconcern;rn rn rn public void setPublish_date(String publish_date)rn rn this.publish_date = publish_date;rn rn rn public void setPrice(float price)rn rn this.price = price;rn rn rn public void setAmount(int amount)rn rn this.amount = amount;rn rn rn public void setRemark(String remark)rn rn this.remark = remark;rn rn rn public String getTitle()rn rn return title;rn rn rn public String getAuthor()rn rn return author;rn rn rn public String getBookconcern()rn rn return bookconcern;rn rn rn public String getPublish_date()rn rn return publish_date;rn rn rn public float getPrice()rn rn return price;rn rn rn public int getAmount()rn rn return amount;rn rn rn public String getRemark()rn rn return remark;rn rn rnrn[/code]rnrn[code=Java]rnpackage db;rnrnimport java.io.Serializable;rnrnpublic class CartItemBean implements Serializablernrn private BookBean book=null;rn private int quantity=0;rn rn public CartItemBean()rn rn rn rn public CartItemBean(BookBean book)rn rn this.book=book;rn this.quantity=1;rn rn rn public void setBook(BookBean book)rn rn this.book = book;rn rn rn public BookBean getBook()rn rn return book;rn rn rn public void setQuantity(int quantity)rn rn this.quantity = quantity;rn rn rn public int getQuantity()rn rn return quantity;rn rn rn rn public float getItemPrice()rn rn float price=book.getPrice()*quantity;rn long val=Math.round(price*100);rn return val/100.0f;rn rnrn[/code]rn在CartItemBean类中调用book.getPrice()方法,为何报错啊:The method getPrice() is undefined for the type BookBean。。。。在同一个包中,都是public。。。为何报没有定义啊???? 论坛

同一个包中的两个调用方法时出现的空指针问题

06-21

如题,用伪代码描述一下rn同一个包下的A,B两个类rn在B中使用A的一个根据id查询list的方法rn结果老是报空指针异常,rn该查询方法在A类中使用正常。rn我在B类中尝试了两种方法都没有成功rn 1,根据A的类名,新建A的实例,然后调用其方法即:rn A a = new A();rn List list = a.find(id);rn 2,通过反射rn Class c = Class.forName("A的全限定名");rn IA(A的接口类) aSevice = (IA)c.newInstance();rn List list = aSevice.find(id);rn 使用这两种方法都在调用这个查询方法时出现空指针异常。rn 最后没有办法,只能把A的方法复制到B中,作为B的方法在调用。rn 很不解,求各位指点一二rn下面是真实代码:rn这相当于B类的代码:rn[code=java] //得到任课教师信息srn// StartClassCheckManagerServiceImpl impl = new StartClassCheckManagerServiceImpl();rn// List teachers = impl.budgetGetTeacherByClassesId(classesId);rn //利用反射rn// Class c = Class.forName("com.fau.technician.service.impl.StartClassCheckManagerServiceImpl");rn// IStartClassCheckManagerService service = (IStartClassCheckManagerService) c.newInstance();rn// List teachers = service.budgetGetTeacherByClassesId(classesId);[/code]rn这相当与A类的代码:rn[code=java]@Overridern public List budgetGetTeacherByClassesId(String classesId) rn rn @SuppressWarnings("unchecked")rn List list = (List)this.iBaseDao.getListByHql("FROM TcClassesAndTeacher t WHERE t.classesId = ? ",null, classesId);rn rn if(null != list && list.size() > 0) rn rn //设置教师姓名及费用rn for(TcClassesAndTeacher tec : list) rn TcAdmin tcTeacher = (TcAdmin)this.iBaseDao.getObject(TcAdmin.class, tec.getTeacherId() );rn tec.setTeacherName(tcTeacher.getName());rn tec.setSumFee(tec.getLessionFee().multiply(new BigDecimal(tec.getClassHours())));rn rn rn return list;rn rn [/code] 论坛

求助:关于同一个包中的不同调用问题

10-22

求助:关于同一个包中的不同类的调用问题rnrn我首先创建了一个工程,然后new了一个Application 即创建了一个Frame1 extends Framern在Frame1类中定义了很多的可视化组件,如jTextField,jButton之类的rnrn然后我又new 了一个Class 名为sqlEditClass,定义如下:rnpackage untitled8;rnimport java.sql.*;rnrnpublic class SqlEditClass rn Frame1 frame1;rn String changeSql="update FriendPhoneNumber set User_name=?, Friend_Phone_Number=? where User_name=? and Friend_Phone_Number=?";rnrn public SqlEditClass() rn // frame1.jTextField21.setText(frame1.jdbLabel1.getText());rn // frame1.jTextField22.setText(frame1.jdbLabel2.getText());rn // frame1.jTextField21.requestFocus();rn rn public void saveSqlEditClass() throws SQLException,ClassNotFoundExceptionrn tryrn Class.forName(Frame1.driverName);rn Connection sqlEditClassConnection= DriverManager.getConnection(Frame1.sourceURL[0]);rn PreparedStatement updatePreparedStatement=sqlEditClassConnection.prepareStatement(changeSql);rn updatePreparedStatement.setString(1,frame1.jTextField21.getText());rn updatePreparedStatement.setString(2,frame1.jTextField22.getText());rn updatePreparedStatement.setString(3,frame1.jdbLabel2.getText());rn updatePreparedStatement.setString(4,frame1.jdbLabel3.getText());rn updatePreparedStatement.executeUpdate();rn rn catch(ClassNotFoundException cnfe)rn catch(SQLException sqle)rn rnrn然后我在Frame1类中的jButton1的JButton1_actionPerformed中调用SqlEditclass类rn的方法savaSqlEditClass(),编译运行都很成功,但是最终数据库中的数据并没有改变rn请问个为大哥这是为什么呀?好象根本就没有调用一样。rnrn里面的控件定义都不会有问题!rnrnrnrnrnrnrn 论坛

在一个包中调用包中的接口和,编译的时间出现问题,应该怎么解决???

08-04

这是Command.java接口rnpackage gallery;rnrnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnrnimport javax.servlet.ServletException;rnrnimport java.io.IOException;rnrnpublic interface Command rn public void handle(HttpServletRequest req, HttpServletResponse res) rn throws ServletException, IOException;rnrn下面是要调用该接口的类rnpackage gallery;rnrnimport javax.servlet.ServletConfig;rnimport javax.servlet.ServletContext;rnimport javax.servlet.http.HttpServlet;rnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnrnimport javax.servlet.ServletException;rnimport javax.servlet.RequestDispatcher;rnrnimport java.io.IOException;rnrnimport java.util.Map;rnimport java.util.HashMap;rnimport java.util.Iterator;rnrnpublic class ControllerServlet extends HttpServlet rn private static String INVENTORY_ROOT;rnrn private Map commands = new HashMap();rn private Inventory inventory = null;rnrn public void init() throws ServletException rn // initialize pathrn ServletConfig config = this.getServletConfig();rn ServletContext context = config.getServletContext();rn INVENTORY_ROOT = context.getRealPath("/images");rn rn // initialize inventory.rn inventory = new Inventory(INVENTORY_ROOT,rn "images/");rnrn // setup all commands.rn commands.put("GET_IMAGES", new GetImagesCommand());rn rnrn public void doGet(HttpServletRequest req, HttpServletResponse res) rn throws ServletException, IOException rn doPost(req, res);rn rnrn public void doPost(HttpServletRequest req, HttpServletResponse res) rn throws ServletException, IOException rnrn logmsg("doPost");rn // Fetch command-name from requestrn String commandName = req.getParameter("COMMAND");rnrn // Fetch command from command-namern Command c = (Command)commands.get(commandName);rnrn // handle command.rn c.handle(req, res);rn rnrnrn private class GetImagesCommand implements Command rn public void handle(HttpServletRequest req, HttpServletResponse res) rn throws ServletException, IOException rn logmsg("command->handle");rnrn String catalog = req.getParameter("CATALOG");rn String numRows = req.getParameter("ITEMS_PER_ROW");rnrn // Fetch images from catalog.rn Iterator images = inventory.getImagesFor(catalog);rnrn // Set data for view.rn req.setAttribute("IMAGES", images);rn req.setAttribute("ITEMS_PER_ROW", numRows);rnrn // Forward to the view.rn RequestDispatcher rd = req.getRequestDispatcher("/galleryViewer");rn rd.forward(req, res);rn rn rnrn private void logmsg(String s) rn System.err.println("logmsg: " + s);rn rnrn出现错误如下ContollerServlet.java:58 cannot resolve symbolrnsymbol: class Commandrnlocation: class gallery.ContollerServlet.GetImagesCommandrnprivate class GetImagesCommand implements Command rn及其他要用到Command类的地方和有到其他包中类的地方!这是一个书上的例子,但我编译不了,希望高手多多指点!!!谢谢谢谢谢谢rn 论坛

没有更多推荐了,返回首页