一个匿名类如何扩展一个超类或实现一个接口?
有没有办法从Java中的匿名内部类访问调用者范围的variables? 以下是示例代码,了解我需要什么: public Long getNumber(final String type, final String refNumber, final Long year) throws ServiceException { Long result = null; try { Session session = PersistenceHelper.getSession(); session.doWork(new Work() { public void execute(Connection conn) throws SQLException { CallableStatement st = conn.prepareCall("{ CALL PACKAGE.procedure(?, ?, ?, ?) }"); st.setString(1, type); st.setString(2, refNumber); st.setLong(3, year); st.registerOutParameter(4, OracleTypes.NUMBER); st.execute(); […]
是否有可能传递参数,或访问外部参数到一个匿名类? 例如: int myVariable = 1; myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // How would one access myVariable here? } }); 是否有任何方法让侦听器访问myVariable或传递myVariable而不创build侦听器作为实际的命名类?
我正在阅读有关新function: http : //www.javaworld.com/article/2078836/java-se/love-and-hate-for-java-8.html 我看到下面的例子: 使用匿名类: button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.out.println("Action Detected"); } }); 随着Lambda: button.addActionListener(e -> { System.out.println("Action Detected"); }); 如果他们想要在匿名类中实现多个方法,有人会用MouseListener做什么,例如: public void mousePressed(MouseEvent e) { saySomething("Mouse pressed; # of clicks: " + e.getClickCount(), e); } public void mouseReleased(MouseEvent e) { saySomething("Mouse released; # of clicks: " + e.getClickCount(), e); […]
我今天遇到了以下问题,而且我想知道是否有针对我的问题的解决scheme。 我的想法是build立匿名类,并将其用作WinForm BindingSource的数据源: public void Init() { var option1 = new { Id = TemplateAction.Update, Option = "Update the Templates", Description = "Bla bla 1." }; var option2 = new { Id = TemplateAction.Download, Option = "Download the Templates", Description = "Bla bla 2." }; var list = new[] {option1, option2}.ToList(); bsOptions.DataSource = list; // […]
Java中匿名类的用法是什么? 我们可以说使用匿名类是Java的好处之一吗?
a只能在这里决赛。 为什么? 如何在onClick()方法中重新分配a方法而不将其保留为私有成员? private void f(Button b, final int a){ b.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { int b = a*5; } }); } 点击时如何返回5 * a ? 我的意思是, private void f(Button b, final int a){ b.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { int b = a*5; return b; // but […]